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

Ale*_*lex 9 notifications observers selector ios swift

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

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

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

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

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

#selector(keyboardWillShow)
Run Code Online (Sandbox Code Playgroud)

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

PS我可以把整个代码放在那里,但我不想要问题非常大,所以我会编辑问题,如果有人问...

Kru*_*nal 13

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

斯威夫特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
    }
}
Run Code Online (Sandbox Code Playgroud)


斯威夫特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)")

} 
Run Code Online (Sandbox Code Playgroud)



根据语言支持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
        }
    }
Run Code Online (Sandbox Code Playgroud)


斯威夫特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
        }
    }
Run Code Online (Sandbox Code Playgroud)