Handle Keyboard Done pressed event on Iphone
I开发者_如何学C am moving my view when a text field is pressed in order to get proper view when keyboard appears. Now, when the Done keyboard button is pressed, I would like to return the view to its initial state. How do I handle an action when the done keyboard button is pressed?
The proper way to do this is to observe to the notifications UIKeyboardDidShowNotification
and UIKeyboardDidHideNotification
as detailed in Apple's documentation.
If you want to know when the Done
button has been pressed, implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
In your delegate. It should be called when the return button is pressed. See API documentation for more details.
Make an IBAction
and connect it to the text field's didEndOnExit
method. Then within the implementation of this method you should put [yourTextFieldOutlet resignFirstResponder];
, which will deactivate the text field.
@freespace has it right, this is all you need to do.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
Tap the done button and poof, the keyboard is gone.
If you just want to know that Done was pressed, you can ask to be told of that control event:
[textField addTarget:self
action:@selector(donePressed)
forControlEvents:UIControlEventEditingDidEndOnExit];
That's the code version of IB's didEndOnExit
.
精彩评论