如何使用string中的参数创建Selector

时间:2017-06-09 07:19:55

标签: ios swift notifications selector observers

我正在使用Swift 3.1和Xcode 8.3.3编写程序。我想创建一个类,负责在键盘出现和消失时移动整个视图。但是我遇到了使用string中的参数创建自定义Selector的困难。要显示或隐藏键盘,我们需要功能:

func keyboardWillShow(notification: Notification) {
//Code moving view when keyboard appears
}

我正在尝试创建一个这样的选择器:

let selector = Selector(("keyboardWillShow")
NotificationCenter.default.addObserver(view, selector: selector, name: .UIKeyboardWillShow, object: anyView.view.window)

正在编译,但是当键盘出现时,它会崩溃。 因为它是独立的类我不能使用这种结构:

#selector(keyboardWillShow)

因为它将Swift函数转换为Objective-C函数(添加@objc)。 所以问题是:如何使用参数string创建一个Selector表单?

P上。 S.我可以将整个代码放在那里,但我不想让问题变得非常大,所以如果有人问我,我会编辑问题...

2 个答案:

答案 0 :(得分:11)

  

这是你想要的,Selector有字符串类型和Notification参数参数

Swift 4

NotificationCenter.default.addObserver(self, selector: Selector(("showKeyboard:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil)

var keyboardHeight = 0.0
//-------------------
@objc func showKeyboard(_ sender: Notification) {
    keyboardWillShow(sender: sender as NSNotification, adjustHeight: 150)
    print("sender - \(sender)")
}

//-------------------
func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        keyboardHeight = Double(keyboardSize.height)
        // do your calculations
    }
}


Swift 3

NotificationCenter.default.addObserver(view, selector: Selector(("keyboardWillShow:")), name: .UIKeyboardWillShow, object: anyView.view.window)

func keyboardWillShow(_ notification: Notification) { 
       keyboardWillShow(sender: sender as NSNotification, adjustHeight: 150)
        print("sender - \(sender)")

} 



根据语言支持,这里是普通选择器 Swift 4

NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)

var keyboardHeight = 0.0

    //-------------------
    @objc func showKeyboard(sender: NSNotification) {
        keyboardWillShow(sender: sender, adjustHeight: 150)
    }

    //-------------------
    func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            keyboardHeight = Double(keyboardSize.height)
            // your operations here
        }
    }


Swift 3

NotificationCenter.default.addObserver(self, selector: #selector(self.showKeyboard(sender:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)

var keyboardHeight = 0.0

    //-------------------
    func showKeyboard(sender: NSNotification) {
        keyboardWillShow(sender: sender, adjustHeight: 150)
    }

   //-------------------
    func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) {
        if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            keyboardHeight = Double(keyboardSize.height)
            // your operations here
        }
    }

答案 1 :(得分:2)

这种方式有效,

{{1}}

这有助于您达到预期的效果。

相关问题