【发布时间】:2012-06-09 18:36:19
【问题描述】:
我需要控制,在显示键盘并按下完成按钮后,当键盘隐藏时。在 iOS 上隐藏键盘时会触发哪个事件?谢谢
【问题讨论】:
标签: iphone objective-c ios xcode cocoa-touch
我需要控制,在显示键盘并按下完成按钮后,当键盘隐藏时。在 iOS 上隐藏键盘时会触发哪个事件?谢谢
【问题讨论】:
标签: iphone objective-c ios xcode cocoa-touch
是的,使用以下
//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
如果你想知道用户何时按下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!")
}
【讨论】:
您可以收听UIKeyboardWillHideNotification,它会在键盘关闭时发送。
【讨论】: