Issues dismissing keyboard conditionally

I have an app that has a username and password field. I want to validate the input before the the user is allowed to stop editing the field. To do that, I'm using the textFieldShouldEndEditing delegate method. If the input doesn't validate I display a UIAlertView.

This approach works as advertised - the user cannot leave the field if the input doesn't validate.

To have the done button on the keyboard dismiss the keyboard, I call resignFirstResponder on the textfield.

The issue I have is the alert is being called twice. How do I keep the alert from showing twice?

edit for clarification

What is happening is that the alert appears, then another alert appears. I then have to dismiss both windows to fix the input.

Here is the textFieldShouldEndEditing method

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"function called %@",textField);
    if([textField.text length] == 0)
    {
        return YES;
    }
    if(textField == userName)
    {
    if([self userNameValidated:textField.text])
    {
        NSLog(@"name validated");
        NSString *tempDonerName = [[NSString alloc] initWithString:(@"%@",userName.text)];
        //[[NSUserDefaults standardUserDefaults] setObject:tempDonerName forKey:@"name"];
        [tempDonerName release];
        return YES;
    } else {
        NSLog(@"name did not validate");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Username",@"Invalid Username title")
                                                        message:NSLocalizedString(@"Please make sure there are no apostrophes,spaces in the username, and that the username is less than 12 characters",@"Invalid username message")
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text") 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];            

        return NO;
    }

} else if (textField == userPin) {
    if([self userPinValidated:textField.text])
    {
        NSLog(@"pin validated");
        //NSString *tempDonerPin = [[NSString alloc] initWithString:(@"%@",userPin.text)];              
        //[[NSUserDefaults standardUserDefaults] setObject:tempDonerPin forKey:@"pin"];
        //[tempDonerPin release];
        return YES;
    } else {
        NSLog(@"pin did not validate");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Password",@"Invalid Pin title")
                                                        message:NSLocalizedString(@"Please make sure there are no apostrophes in the password",@"Invalid password message")
                                                       delegate:nil
                                              cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text") 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];

        return NO;
    }
}else {
    NSLog(@"code validate - shouldn't get called");
    return YES;
}

}

Answers


Leaving aside the double alert: resignFirstResponder is going to call textFieldShouldEndEditing, there's no way around that. So you need either to pass a flag saying that it's cool to exit the field with unvalidated input, or otherwise clear the content; or make it a user decision and enable the clear button on the field.


Need Your Help

How to trigger [GKLocalPlayerListener player: didRequestMatchWithPlayers:]

ios game-center gamekit

The GKInviteEventListener Protocol contains the following method that must be implemented:

Measuring geographic distance with scipy

python scipy geographic-distance

I fail to use the outcome of scipy's pdist function. I am interested in the real geographic distance (preferred unit: km). Take the following coordinates: