【问题标题】:iOS event when keyboard hides键盘隐藏时的iOS事件
【发布时间】:2012-06-09 18:36:19
【问题描述】:

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

【问题讨论】:

标签: iphone objective-c ios xcode cocoa-touch


【解决方案1】:

是的,使用以下

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

【讨论】:

  • 这将在关闭时触发,而不是在键盘完全隐藏时触发。
  • 是的,正确,请检查更新后的答案,对于完全隐藏的通知,请使用UIKeyboardDidHideNotification
【解决方案2】:

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

斯威夫特 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!")
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 确切地说,通知是在键盘关闭之前发送的。
    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2013-06-16
    • 2023-04-05
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多