Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a method for didEndOnExit event of UITextField

How do I programmatically assign a method (observer?) to the didEndOnExit event of a UITextField object?

This is easy enough to do in IB but I can't figure out how to do it in code.

like image 225
Frank V Avatar asked May 14 '09 04:05

Frank V


People also ask

Which method can be used to dismiss the system keyboard if a Uitextfield or Uitextview is currently being edited?

You can ask the system to dismiss the keyboard by calling the resignFirstResponder() method of your text field. Usually, you dismiss the keyboard in response to specific interactions. For example, you might dismiss the keyboard when the user taps the keyboard's return key.

How do you dismiss keyboard when tapping outside textfield?

At the keyboard settings tab, select configure input methods. At Android keyboard, select Settings. Uncheck Sound on keypress. Done.


3 Answers

I just figured it out...

[mytextField addTarget:self 
        action:@selector(methodToFire:)
        forControlEvents:UIControlEventEditingDidEndOnExit];
like image 98
Frank V Avatar answered Oct 20 '22 18:10

Frank V


In your view controller implement the following method:

- (void)textFieldDidEndEditing:(UITextField *)textField{

//do stuff

}

Don't forget to set the delegate in viewDidLoad or the initializer:

myTextField.delegate = self;
like image 24
Corey Floyd Avatar answered Oct 20 '22 18:10

Corey Floyd


For anyone considering to use this target to make another textfield becomeFirstResponder to create a 'Next' button option that feels like you are 'tabbing' through textFields I would recommend using textFieldShouldReturn instead with code like this. Let me tell you it works so well you'll feel like you're riding a unicorn:

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];

    if([textField isEqual:_textField1]){
        [_textField2 becomeFirstResponder];
        return NO;
    }else if([textField isEqual:_textField2]){
        [_textField3 becomeFirstResponder];
        return NO;
    }

    return YES;
}
like image 37
Chris Klingler Avatar answered Oct 20 '22 18:10

Chris Klingler