在performSelector中包含dismissWithClickedButtonIndex后,UIAlertView会重新出现:withObject:afterDelay:

时间:2012-12-19 18:24:10

标签: ios iphone ios5 uialertview

如果密码正确,我有一个按钮,我想在触发segue之前用密码实现。当你输入错误的密码并且我已经实现了另一个alertView告诉用户密码错误时,它看起来都很好。 警报视图弹出并在延迟一段时间后解散,它会不断重新出现并消失,屏幕上无法完成任何其他工作! 如何阻止重新出现? 以下是我处理此问题的代码部分:

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

2 个答案:

答案 0 :(得分:1)

[_wrongPassword dismissWithClickedButtonIndex:-1 animated:YES];将调用委托方法alertView:didDismissWithButtonIndex:

您有两种选择:

  1. 不要在错误的密码提示上设置委托

  2. 检查alertView:didDismissWithButtonIndex:中的正确提醒,例如

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

答案 1 :(得分:0)

问题导致,因为当您解除错误的密码提醒时,它也会调用didDismissWithButtonIndex委托方法。

解决方案1 ​​

将错误密码提醒的delegate设置为nil

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

解决方案2

在alertView中添加标记。并改变你的方法,如:

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


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 7)
    {
       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]];
      }
   }
}