0

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

2 回答 2

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
        }
    }
    
于 2012-12-19T18:33:24.947 回答
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]];
      }
   }
}
于 2012-12-19T18:34:13.893 回答