如何使用string参数创buildselect器

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

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

我想创build一个这样的select器:

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

它正在编译,但当键盘出现时,它崩溃。 因为它是独立的class级我不能使用这个build筑:

 #selector(keyboardWillShow) 

因为它将Swift函数转换为Objective-C函数(添加@objc)。 所以问题是:如何创build一个select器forms的参数string?

感谢您的任何帮助!

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

这是你想要的,select器的stringtypes和通知参数参数

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)") } 

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

根据语言支持,这里是普通的select器
斯威夫特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 } } 

这种方式工作,

 override func viewDidLoad() { super.viewDidLoad() // put it wherever you want NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showKeyboard), name:UIKeyboardWillShowNotification, object: nil) } func showKeyboard(sender: NSNotification) { keyboardWillShow(sender, adjustHeight: 150) } func keyboardWillShow(sender: NSNotification, adjustHeight: CGFloat) { if let keyboardSize = (sender.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { keyboardHeight = keyboardSize.height // do your calculations } } 

这将帮助您达到预期的结果。