Issues dismissing keyboard conditionally

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-03-30T16:20:35Z Indexed on 2010/03/30 22:33 UTC
Read the original article Hit count: 475

Filed under:
|
|

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;
}

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c