3

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

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

2 回答 2

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)
}
于 2016-10-14T02:16:06.653 回答
1

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

于 2017-01-04T11:27:19.907 回答