Ios Swift 3:发送到实例的未识别选择器(KeyboardWillShow)

Ios Swift 3:发送到实例的未识别选择器(KeyboardWillShow),ios,swift,swift3,ios10,notificationcenter,Ios,Swift,Swift3,Ios10,Notificationcenter,我在stack overflow上搜索了很多,但根据他们的解决方案,我的程序和前面提到的一样,但仍然不起作用 func subscribeToKeyboardNotifications() { NotificationCenter.default.addObserver(self, selector:Selector(("keyboardWillShow:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil) }

我在stack overflow上搜索了很多,但根据他们的解决方案,我的程序和前面提到的一样,但仍然不起作用

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector:Selector(("keyboardWillShow:")), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
}


func keyboardWillShow(notification:NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification: notification)
}

选择器的参数应该是
#选择器(keyboardWillShow)
,如下所示:

func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}

func keyboardWillShow(notification:NSNotification) {
    view.frame.origin.y -= getKeyboardHeight(notification: notification)
}

如果您不使用#selector,那么它将给出NSType的未捕获异常,因此它将终止应用程序。

我按照您所说的那样编写了它,但仍然给出了一个错误,并在selector参数上显示警告,没有使用objective-c“keyboardWillShow”声明的方法。我已更新了我的答案。Swift的新语法是:
#选择器(keyboardWillShow)
谢谢buddy@Michael Patzer