TextFieldShouldEndEditing called multiple times

I am working on a view with multiple objects UITextField. My view controller serves as UITextFieldDelegate, and I implemented a method (BOOL)textFieldShouldEndEditing:(UITextField *)textFieldfor saving and checking the displayed record.

If the user clicks the "Finish" button after editing the item and the save / check fails, is displayed UIAlertViewand the user remains on UITextField, which does not perform the check.

My problem is this: when the user presses a button UITextFieldthat cannot save / check on another of UITextFields, then the method (BOOL)textFieldShouldEndEditing:(UITextField *)textFieldis called several times, and UIAlertViewappears several times.

Why (BOOL)textFieldShouldEndEditing:(UITextField *)textFieldis it called once when the user clicks "Finish" on the keyboard, but is called several times when the user clicks on 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;
}
+5
source share
4 answers

I think your problem comes from the order in which the textField methods are called when you edit the text screen and directly click on another.

If I'm not mistaken, it should be something like this (you are editing A and clicking on B)

  • textFieldShouldBeginEditing for field B
  • textFieldShouldEndEditing for field A
  • textFieldDidEndEditing for field A
  • textFieldDidBeginEditing for field B

, textFieldShouldEndEditing, B . , UIAlertView, B , , textFieldShouldEndEditing !

, , textField . , , , boolean, , . TRUE textFieldShouldBeginEditing FALSE textFieldDidBeginEditing. textFieldShouldEndEditing, TRUE, , . (, EndEditing false - ).

+3

- UIAlertView . - :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    double delayInSeconds = 0.;
    self.currentTextField.text = @"Something that won't trigger validation error";
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // do what you need here
    });
}
+1

, 2 . ? ...

-

- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{

if(i_dont_know){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                                                    message:@"Message"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    [alert release];
    return false;
}

return true;}

, UIAlertView funcion "textFieldShouldEndEditing:"...

, - -, "shouldEndEditing" , . "textFieldShouldEndEditing:" - .

- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{

if(shouldEndEditing == false)
{
    shouldEndEditing = true;
    return false;
}

if(i_dont_know){
    shouldEndEditing = false;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                                                    message:@"Message"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    [alert release];
    return false;
}

return true;}

...

0

Could you add different tags to each text view and check the tag in textFieldShouldEndEditing? Or am I missing a point?

0
source

All Articles