Hi all,
I am working on a view that has multiple UITextField objects. My view controller serves as the UITextFieldDelegate, and I've implemented the (BOOL)textFieldShouldEndEditing:(UITextField *)textField
method to save and validate the record being displayed.
If the user clicks on the "Done" button after editing an item and the save/validate fails, then a UIAlertView is displayed and the user is kept on the UITextField that fails validation.
My problem is this -- when a user clicks from the UITextField that will fail save/validation onto another of the UITextFields, then the (BOOL)textFieldShouldEndEditing:(UITextField *)textField
method is called multiple times, and the UIAlertView pops up multiple times.
Why is (BOOL)textFieldShouldEndEditing:(UITextField *)textField
called once when the user clicks "Done" on the keyboard, but called multiple times when the user clicks onto another UITextField?
Here is my code:
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"textFieldShouldEndEditing called by textField with text=%@", textField.text);
currentItem.nameOrNumber = nameOrNumber.text;
// Try to save the managed object.
NSError *error = nil;
if (![[currentItem managedObjectContext] save:&error]) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Uh Oh!",@"")
message:[error localizedDescription]
delegate:self
cancelButtonTitle:NSLocalizedString(@"OK",@"")
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
shouldEnd = NO;
}
return shouldEnd;
}
Thank you! -Ben