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

Swift 3: Unrecognised selector sent to instance (KeyboardWillShow)

我在堆栈溢出上搜索了很多,但根据他们的解决方案,我的程序与提到的相同,但仍然无法正常工作。

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

你的选择器参数应该是 #selector(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 异常,因此它会终止应用程序。