Dailycode.info

Short solution for short problems

On leave and enter hit events in UITextField (iOS)

If you want to catch the event when a user leaves a text box and has changed something, or when he presses enter in the text box itself, you will need to do 2 things:

- For the leave it's very easy, Apple already prepared this event for you. Use the Editing Did End Action and connect it to your controller's implementation file:

 

But now you also want to capture the event that occurs when the user presses enter in the textfield.

For this we start by adding the UITextFieldDelegate protocol in the controller.h:

@interface PhotoTakerViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate, UITextFieldDelegate >

next we implement it like this:

- (BOOL) textFieldShouldReturn:(UITextField *)textField

{

    ...Do something

    return YES;

}