xcode iPad“关闭键盘”按钮(右下角),如何检测何时发生?什么功能告诉我它被按下了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3121037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPad "dismiss keyboard" button (lower right), how do I detect when this occurs? What function tells me it was pressed?
提问by SolidSnake4444
I have an ipad app that when you are in landscape view, the view will move up when the keyboard is brought in. When you press done on the keyboard, textFieldShouldReturn and textFieldShouldEndEditing are called in which case, I move the view down in shouldEndEditing.
我有一个 ipad 应用程序,当你在横向视图中时,当键盘被带入时,视图会向上移动。当你按下键盘上的完成时,textFieldShouldReturn 和 textFieldShouldEndEditing 被调用,在这种情况下,我在 shouldEndEditing 中向下移动视图。
If the user presses the dismiss keyboard button, the keyboard does poof, yet the view is still stuck floating where I moved it.
如果用户按下关闭键盘按钮,键盘会发出噗噗声,但视图仍然停留在我移动它的地方。
I need to know how or what function is called when that button is pressed so I can redirect the function to textFieldShouldEndEditing.
我需要知道按下该按钮时如何或调用什么函数,以便我可以将该函数重定向到 textFieldShouldEndEditing。
Thanks!
谢谢!
回答by Sharjeel Aziz
You can listen for keyboard hide UIKeyboardWillHideNotificationnotification.
您可以监听键盘隐藏UIKeyboardWillHideNotification通知。
Example code is here http://developer.apple.com/iphone/library/samplecode/KeyboardAccessory/Listings/Classes_ViewController_m.html
回答by Chris Allinson
When the "lower keyboard" button is pressed, the textfield delegate method of:
当按下“下键盘”按钮时,文本字段委托方法为:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
Won't be called.
不会叫。
When either the "lower keyboard" or "return button" are pressed, the:
当按下“下键盘”或“返回按钮”时,:
-(void)textFieldDidEndEditing:(UITextField *)textField
Will be called.
会被调用。
I use a variable NSString *lowerKeyboardButtonPressed
initially set to @""
which I set to @"N"
in the textfieldShouldReturn
method ... then in the textFieldDidEndEditing
method I check to see if it is set to @"N"
... then I know if the return key or lower keyboard key was pressed. The last line in my textFieldDidEndEditing
method sets the variable back to @""
.
我使用一个NSString *lowerKeyboardButtonPressed
最初设置为@""
我@"N"
在textfieldShouldReturn
方法中设置的变量......然后在textFieldDidEndEditing
方法中我检查它是否设置为@"N"
......然后我知道是否按下了返回键或下键盘键。我的textFieldDidEndEditing
方法中的最后一行将变量设置回@""
.
回答by Woolie
If you press the keyboard dismiss button and you have a hardware keyboard attached, then the willHide
will only be called if you don't have an input accessory view. At which point you need to adjust in the willShow
as well (which will have a negative difference between the UIKeyboardFrameBeginUserInfoKey
and UIKeyboardFrameEndUserInfoKey
keys).
如果您按下键盘关闭按钮并且连接了硬件键盘,那么willHide
只有在您没有输入附件视图时才会调用。此时您还需要在 中进行调整willShow
(这将在UIKeyboardFrameBeginUserInfoKey
和UIKeyboardFrameEndUserInfoKey
键之间产生负差异)。