开发者

Handling 'Done' button on the keyboard outside of textViewDidEndEditing

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 a开发者_Python百科ny XIB for my view, by the way.

Is this possible?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜