如何将NSNotification作为参数传递给Swift中的选择器函数

时间:2015-02-04 16:35:24

标签: ios swift nsnotifications

我试图在键盘出现时尝试实现UIKeyboardWillShowNotification来处理我的观看位置。 我加上我的观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow", name: UIKeyboardWillShowNotification, object: nil)

然后我有keyboardWillShow功能:

func keyboardWillShow(notification: NSNotification){
    //Need to access to the notification here
}

在函数keyboardWillShow中,我需要收到NSNotification才能访问用户信息,但我收到此错误:

  

"无法识别的选择器发送到实例0x7fd993e7d020"

1 个答案:

答案 0 :(得分:7)

你忘了冒号:。将其更改为:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)