View Full Version : Handling the Done key on the keyboard.

Aug 20, 2008, 01:07 PM
How can I execute code after the user hits the done key.

Aug 20, 2008, 01:47 PM

A question I can answer!

So now the flow of information won't be so one-way!

You need to set your controller object to be the delegate of the text field first.

So... for example...

textField.delegate = self

I put that in the awakeFromNib method for my controller... and you need to do that for each text field.

Then you also need a method called this:

- (BOOL) textFieldShouldReturn: (UITextField *) textField
// Insert the code you want to execute here.

Finally, in the header file of the object you want to be the controller, you need

@interface name of your controller object : NSObject <UITextFieldDelegate>

so that you won't get errors about not having the right protocols implemented.

Oh... one last thing... if you're having multiple text fields that you want to do different things... you might want an if-else in your code.


- (BOOL) textFieldShouldReturn: (UITextField *) textField
if (textField == nameField)
// Code that you want it to do if nameField has just had its done button hit.

else if (textField == otherField)
// Code that you want it to do if the otherField has just had its done button hit.

... Hopefully you understood that all... and hopefully I told you the right things...

Aug 20, 2008, 02:01 PM
Cool thanks man