Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seVor

macrumors newbie
Original poster
Aug 20, 2008
4
0
How can I execute code after the user hits the done key.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,616
6,145
Huzzah!

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...

Code:
[i]textField[/i].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:

Code:
- (BOOL) textFieldShouldReturn: (UITextField *) textField
{
// Insert the code you want to execute here.
}
[/code]

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.

Like...

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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.