25

我需要控制,在显示键盘并按下完成按钮后,当键盘隐藏时。在 iOS 上隐藏键盘时会触发哪个事件?谢谢

4

3 回答 3

59

是 使用以下

//UIKeyboardDidHideNotification when keyboard is fully hidden
//name:UIKeyboardWillHideNotification when keyboard is going to be hidden

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];

onKeyboardHide

-(void)onKeyboardHide:(NSNotification *)notification
{
     //keyboard will hide
}
于 2012-06-05T18:06:43.950 回答
6

如果你想知道用户何时按下完成按钮,你必须采用UITextFieldDelegate协议,然后在你的视图控制器中实现这个方法:

斯威夫特 3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // this will hide the keyboard
    textField.resignFirstResponder()
    return true
}

如果您想简单地知道键盘何时显示或隐藏,请使用Notification

斯威夫特 3:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: .UIKeyboardWillShow , object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: .UIKeyboardWillHide , object: nil)

func keyboardWillShow(_ notification: NSNotification) {
    print("keyboard will show!")

    // To obtain the size of the keyboard:
    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size

}

func keyboardWillHide(_ notification: NSNotification) {
    print("Keyboard will hide!")
}
于 2017-01-06T19:15:59.120 回答
5

您可以收听 a UIKeyboardWillHideNotification,它会在键盘关闭时发送。

于 2012-06-05T18:06:22.283 回答