0

I have a few UITextFields (within UITableViewCells) on my UIView with a "Save" UIButton. I want to do some basic validation on the UITextFields when the user clicks the "Save" button.

I have overridden textFieldDidEndEditing to save each of my UITextField data to an instance variable; however, if a user clicks the save button before either clicking the "Return" button of the UIKeyboard or clicking on another UITextField the data in my last UITextField is never saved to my instance variable and validation always fails.

I am looking for a way to trigger an "onBlur" (I know that's a JS thing)-type event to save my string in UITextField to my instance variable.

I've looked through the UITextFieldDelegate Protocol and I do not see anything like this.

Is there a method I may be missing?

El Guapo
  • 5,581
  • 7
  • 54
  • 82

2 Answers2

1

to trigger textFieldDidEndEditing on your UITextField, you will need to call

[_txt resignFirstResponder];

were _txt is your UITextField

Please note that if you dont have a reference to _txt and you need to find the first responder in order to resign it You could use the solution from this question Get the current first responder without using a private API

Then instead of calling

[_txt resignFirstResponder];

you would call

[self.view findAndResignFirstResponder];
Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
1

Try this // if we encounter a newline character return

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{   
    // enter closes the keyboard
    if ([string isEqualToString:@"\n"])
    {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}

which will trigger

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    // Call webservice
    return YES;
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Prabhu.Somasundaram
  • 1,380
  • 10
  • 13
  • Please do not use signatures/taglines in your posts. Your user box counts as your signature, and you can use your profile to post any information about yourself you like. [FAQ on signatures/taglines](http://stackoverflow.com/faq#signatures) – Andrew Barber Jan 25 '13 at 06:07