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 UITextField
s, 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;
}
Couldn't you add different tags in each textview and check the tag in the textFieldShouldEndEditing? Or have I missed the point?
Looks right for me to be called 2 times for each test field. Why? just think about... had passed to me also and get me headache
You can NOT do some like this
Is that the UIAlertView show is also trying to resign the editing of text field and calling this funcion "textFieldShouldEndEditing:"...
So my way to solve this was to add a member variable called "shouldEndEditing" in the interface delaration, who is true por default. And after the "textFieldShouldEndEditing:" can be some like this.
Good luck...
I think your problem comes from the order in which textField methods are called when you are editing a textField and directly tap onto another.
If I am not mistaken, it should be something like this (you are editing on A and tap on B)
textFieldShouldBeginEditing
for field BtextFieldShouldEndEditing
for field AtextFieldDidEndEditing
for field AtextFieldDidBeginEditing
for field BSo when you are in
textFieldShouldEndEditing
method, textfield B already has become the first responder. So when you make the UIAlertView appear, B loses focus and thus callstextFieldShouldEndEditing
too!This also has been a problem for me when I wanted to raise a view when a textField started editing. The solution I found was to create a boolean class variable indicating whether or not I am currently switching from one textField to another. I set it to
TRUE
intextFieldShouldBeginEditing
and toFALSE
intextFieldDidBeginEditing
. When you're intextFieldShouldEndEditing
, if it is set toTRUE
it means the user directly tapped on another textField. Then you just have to find the right way to make your tests only once (maybe shouldEndEditing should return false or something).Another option is to let the
UIAlertView
fake a correct validation and defer the correcting part to a latter time. Something like this :