Search code examples
iosiphoneios5uialertview

UIAlertView keeps re-appearing after dismissWithClickedButtonIndex included in performSelector: withObject: afterDelay:


I have a button which I want to implement with password before triggering a segue if the password is correct. it all looks fine up to the moment when you type in wrong password and I have implemented another alertView to tell the user the password is wrong. When the alert view pops out and dismisses after some delay, it keeps re-appearing and disappearing and nothing else can be done on the screen! How to stop the re appearing? Below is my part of the code that deals with this:

- (IBAction)editLeagues:(id)sender {

    [self presentAlertViewForPassword];

}

-(void)presentAlertViewForPassword
{

    _passwordAlert = [[UIAlertView alloc]initWithTitle:@"Password"
                                                           message:@"Enter Password to edit Leagues"
                                                          delegate:self
                                                 cancelButtonTitle:@"Cancel"
                                                 otherButtonTitles:@"OK", nil];
    [_passwordAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
    _passwordField = [_passwordAlert textFieldAtIndex:0];
    _passwordField.delegate = self;
    _passwordField.autocapitalizationType = UITextAutocapitalizationTypeWords;
    _passwordField.tag = textFieldPassword;
    [_passwordAlert show];

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSString *password = [NSString stringWithFormat:@"55555"];

      if ( ![_passwordField.text isEqual:password]) {

          _wrongPassword = [[UIAlertView alloc] initWithTitle:@"Wrong Password"
                                                                message:@"You are not authorised to use this feature!"
                                                               delegate:self
                                                      cancelButtonTitle:nil
                                                      otherButtonTitles:nil];
        [_wrongPassword show];

        [self performSelector:@selector(allertViewDelayedDissmiss:) withObject:nil afterDelay:2];
    }
    else
    {
         [self performSegueWithIdentifier:@"addLeague" sender:[alertView buttonTitleAtIndex:0]];
    }

}

-(void) allertViewDelayedDissmiss:(UIAlertView *)alertView
{
    [_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES];

}


- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] >= 4 )
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

Solution

  • [_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES]; will call the delegate method alertView:didDismissWithButtonIndex:

    You have two options:

    1. don't set a delegate on the wrong password alert

    2. check for the correct alert in alertView:didDismissWithButtonIndex: e.g.

      - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
      {
          if (alert == _passwordAlert) {
              NSString *password = [NSString stringWithFormat:@"55555"];
              // and so on
          }
      }