Pregunta

I have a UIAlertView with UIAlertViewStyleSecureTextInput. When the user taps "Login" and the password is correct, it will push the next ViewController. If the password is incorrect, it displays another UIAlertView prompting a single "Dismiss" button. What I am trying to do is when the user taps the return key, the "Login" button will be triggered. As it sits now, when the return key is pressed the alert just dismisses, regardless if the password is correct or not. Maybe there is a more logical solution than what I have attempted? Sorry if the title is a little confusing, I don't know how else to explain it. Any help is greatly appreciated. Thanks.

I have declared the first alert in my .h file, and conformed to the UIAlertViewDelegate and UITextFieldDelegate:

@interface EndViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>

@property (retain, strong) UIAlertView *loginRequiredMsg;

Method for the "Login" alert:

- (IBAction)resultsBtnPressed:(UIButton *)sender {
    self.loginRequiredMsg = [[UIAlertView alloc]initWithTitle:@"Login Required"
                                                     message:@"Please enter the admin password"
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"Login", nil];
    self.loginRequiredMsg.alertViewStyle = UIAlertViewStyleSecureTextInput;
    [self.loginRequiredMsg textFieldAtIndex:0].delegate = self;
    [self.loginRequiredMsg show];
}

Method for dismissing the keyboard when return key is tapped (I think I need to somehow call the next alert here if the password is incorrect, as currently it just dismisses the alert):

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Dismiss keyboard when return key is pressed
    [self.loginRequiredMsg dismissWithClickedButtonIndex:self.loginRequiredMsg.firstOtherButtonIndex animated:YES];
    return YES;
}

And finally the method containing the outcome of the password entered:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Login"]) {
        UITextField *password = [alertView textFieldAtIndex:0];

        // Basic login authentication
        if ([password.text isEqualToString:@"admin"]) {
            // Allocate & initialise ViewController
            ResultsViewController *Results = [[ResultsViewController alloc]initWithNibName:@"ResultsViewController" bundle:nil];

            // Push next ViewController
            [self.navigationController pushViewController:Results animated:YES];
            NSLog(@"Show results");

        } else {
            UIAlertView *errorMsg = [[UIAlertView alloc]initWithTitle:@"Error"
                                                             message:@"Admin password is incorrect. Please try again."
                                                            delegate:self
                                                   cancelButtonTitle:@"Dismiss"
                                                   otherButtonTitles:nil];
            [errorMsg show];
        }
    }
}
¿Fue útil?

Solución

As discussed in the comments, you need to implement alertView:didDismissWithButtonIndex: which tells you when the alert view has been dismissed. When you tap the return key, it calls that method with the index of the OK button.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top