Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 键盘隐藏时的iOS事件_Iphone_Objective C_Ios_Xcode_Cocoa Touch - Fatal编程技术网

Iphone 键盘隐藏时的iOS事件

Iphone 键盘隐藏时的iOS事件,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我需要控制,在键盘显示并按下“完成”按钮后,当键盘隐藏时。在iOS上隐藏键盘时触发哪个事件?谢谢您可以收听UIKeyboardWillHideNotification,它会在键盘关闭时发送。是使用以下命令 //UIKeyboardDidHideNotification when keyboard is fully hidden //name:UIKeyboardWillHideNotification when keyboard is going to be hidden [[NSNotifi

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

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

是使用以下命令

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

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

Swift 3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // this will hide the keyboard
    textField.resignFirstResponder()
    return true
}
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!")
}
如果您只想知道键盘何时显示或隐藏,请使用
通知

Swift 3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // this will hide the keyboard
    textField.resignFirstResponder()
    return true
}
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!")
}

准确地说,通知是在键盘被解除之前发送的。可能重复的通知将在解除时触发,而不是在键盘完全隐藏时触发。是的,正确,请检查更新的答案,以获取完全隐藏的通知使用
uikeyboarddidenotification