Ios UIAlertView在使用performSelector:withObject:afterDelay中包含的Clicked Button索引解除后继续重新显示:

Ios UIAlertView在使用performSelector:withObject:afterDelay中包含的Clicked Button索引解除后继续重新显示:,ios,iphone,ios5,uialertview,Ios,Iphone,Ios5,Uialertview,我有一个按钮,如果密码正确,我想在触发segue之前用密码实现它。当你输入错误的密码时,一切看起来都很好,我已经实现了另一个alertView来告诉用户密码是错误的当警报视图弹出并在延迟一段时间后解除时,它会不断重新出现和消失,并且在屏幕上无法执行任何其他操作 如何阻止重演? 下面是我处理此问题的代码部分: - (IBAction)editLeagues:(id)sender { [self presentAlertViewForPassword]; } -(void)presen

我有一个按钮,如果密码正确,我想在触发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;
    }
}

[\u错误密码使用Clicked按钮解除索引:-1动画:是]
将调用委托方法
alertView:didDismissWithButtonIndex:

您有两个选择:

  • 不要将代理设置为错误的密码警报

  • 检查
    alertView:didDismissWithButtonIndex:
    中的警报是否正确,例如

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

  • 问题的原因是,当您解除错误的密码警报时,它还会调用
    didDismissWithButtonIndex
    delegate方法

    解决方案1

    将错误密码警报的
    代表设置为
    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]];
          }
       }
    }
    

    谢谢@Matthias,但当我这么做时,错误的密码警报会显示一秒钟,然后消失!这样,用户将无法看到消息。我的目的是向用户显示错误所在,这就是为什么我添加了延迟方法,该方法工作正常,它延迟窗口消失,但在它消失后,它再次出现,并不断重复。您仍然可以使用延迟方法。只是不要将代理设置为错误的密码警报,或者检查如果alertView==\u PasswordAlertThank@Matthias,您是否只调用检查密码的代码这帮了我很多忙只是一个小问题-如果我在第一个警报上按取消按钮而不键入任何密码,它会弹出错误的密码警报:)但我会简单地修复:)如果(buttonIndex!=alertView.cancelButtonIndex){/***/}