Reputation:
When "Done" is pressed I know textViewDidEndEditing:
is called. It is also sometimes called by other actions.
I want a method that is only called when "Done" is pressed -- exclusively. I've been reading around for snippets of code, but don't see anybody doing this. I am not using any XIB for my view, by the way.
Is this possible?
Upvotes: 7
Views: 9242
Reputation: 15617
An instance of UITextField will send a textFieldShouldReturn:
message to its delegate whenever the Return key is pressed. If you want your controller to receive this message, have it send a setDelegate:
message to the text field, passing self
as the argument (or connect the text field's delegate outlet to the controller in Interface Builder) and implement the following method in your controller class:
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
See the documentation for the UITextFieldDelegate
protocol for further information.
Upvotes: 14
Reputation: 12345
Firstly, the textFieldDidEndEditing is called not when the user taps the DONE button. It is called as soon as you touch anything after editing.
For having an exclusive button 'Done' you can create a Done key as a UIBarButtonItem, and write an IBAction like
- (IBAction) doneClicked:(id)sender
The following line assigns the Done key as the Return Key.
textField.returnKeyType = UIReturnKeyDone;
You can check if the Done key was pressed in the TextFieldDidEndEditing...You may also use another button, and return it instead.
Upvotes: 1