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

时间:2016-10-14 02:11:48

标签: ios swift swift3 ios10 notificationcenter

我在堆栈溢出时搜索了很多关于它的信息,但根据他们的解决方案,我的程序与提及的相同,但仍无效。

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

2 个答案:

答案 0 :(得分:6)

您对选择器的参数应为#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)
}

答案 1 :(得分:1)

如果您没有使用#selector,那么它会给NSType带来未捕获的异常,因此会终止该应用。