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

问题描述 投票:0回答:2

如果密码正确,我有一个按钮,我想用密码实现,然后触发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;
    }
}
ios iphone ios5 uialertview
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 } }

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]];
      }
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.