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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在performSelector中包含dismissWithClickedButtonIndex后,UIAlertView会重新出现:withObject:afterDelay:相关的知识,希望对你有一定的参考价值。

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

[_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 } }
另一答案

问题导致,因为当您解除错误的密码警报时,它也会调用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]];
      }
   }
}

以上是关于在performSelector中包含dismissWithClickedButtonIndex后,UIAlertView会重新出现:withObject:afterDelay:的主要内容,如果未能解决你的问题,请参考以下文章

如何在 GNUMakefile 中包含 libxml2 库

如何在 SELECT 部分中包含 BIT 类型列而不在 T-SQL 中的 GROUP BY 中包含它?

在 tampermonkey(userscript) 中包含所有页面

如何在 ConfigurationSection 中包含简单集合

PHP 在函数中包含/要求

如何在 SwiftUI 中包含和调用 BSImagePicker