【问题标题】:UIAlertview is not calling the right functionUIAlertview 没有调用正确的函数
【发布时间】:2013-11-15 18:46:49
【问题描述】:

我有这个 UIAlertview 带有我试图用来调用函数的标签。出现fogotpassword 警报视图,然后出现重置警报视图,但是当我尝试调用 NSLog(@"Password");通过按下重置警报视图中的第一个按钮来运行它不会被调用。相反,reset alertview 按钮再次弹出。我将不胜感激。

-(void)viewDidLoad{
    forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error"
                                                             message:@"Your login credentials do not match"
                                                            delegate:self
                                                   cancelButtonTitle:@"Try Again"
                                                   otherButtonTitles: @"Forgot Password",nil];
    [forgotPassword show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    forgotPassword.tag = 1;
    resetPassword.tag = 2;

    if (buttonIndex == 1 && forgotPassword.tag ==1)
    {

        resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
                                                            message:@"Email"                                                                                                            delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Reset Password", nil];
        [resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];

        [resetPassword show];

        NSLog(@"RESET");

    }
    if (buttonIndex == 1 && resetPassword.tag == 2) {
       NSLog(@"Password");
    }
}

【问题讨论】:

    标签: ios objective-c uialertview


    【解决方案1】:

    你的逻辑全乱了。您设置了两个标签,因此两者都将始终为真。由于您似乎有两个不同警报视图的 ivars,请去掉标签并执行以下操作:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (alertView == forgotPassword) {
            if (buttonIndex == alertView.firstOtherButtonIndex) {
                // show other alert
            }
        else if (alertView == resetPassword) {
            if (buttonIndex == alertView.firstOtherButtonIndex) {
                // reset
            }
        }
    }
    

    【讨论】:

    • 谢谢,时间一到就会给你打勾。我只是按照另一个 stackoverflow 答案,我想这不是最好的方法
    • 请记住,在警报视图中使用标签而不是 ivars 也很好。您只需要在创建警报视图时设置标签,而不是在委托方法中。
    【解决方案2】:

    无需使用标签

    这样试试

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView==forgotPassword)
        NSLog(@"forgotPassword alert");
    }
    

    【讨论】:

      【解决方案3】:

      您的代码很混乱。您创建一个警报视图“forgotPassword”并显示它,而不设置它的标签。然后,在您的 alertView:clickedButtonAtIndex: 方法中,您在两个警报视图上显式设置标签,然后询问这些警报视图以查看它们的标签是什么。这些标签永远不会改变。

      相反,您应该在显示每个警报视图之前为其设置标签,然后检查传递给 alertView:clickedButtonAtIndex: 方法的 UIAlertView 的标签。像这样的:

      -(void)viewDidLoad{
        forgotPassword = [[UIAlertView alloc] initWithTitle:@"Login Error"
                                                                   message:@"Your login credentials do not match"
                                                                  delegate:self
                                                         cancelButtonTitle:@"Try Again"
                                                         otherButtonTitles: @"Forgot Password",nil];
          forgotPassword.tag = 1;
          [forgotPassword show];
       }
      
       - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
      
      if (buttonIndex == 1 && alertView.tag ==1)
      {
      
          resetPassword = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
                                                                  message:@"Email"                                                                                                            delegate:self
                                                        cancelButtonTitle:@"Cancel"
                                                        otherButtonTitles:@"Reset Password", nil];
          [resetPassword setAlertViewStyle:UIAlertViewStylePlainTextInput];
          resetPassword.tag = 2;
          [resetPassword show];
      
          NSLog(@"RESET");
      
      }
      if (buttonIndex == 1 && alertView.tag == 2) {
         NSLog(@"Password");
      }
      

      }

      【讨论】:

        【解决方案4】:

        您应该在方法中使用 alertView.tag == 1 和 alertView.tag == 2。正如您现在所拥有的,只要 buttonIndex == 1,两个 if 语句都将始终为真,因为您明确设置了 forgotPassword.tag = 1 和 resetPassword.tag = 2,然后检查每个项目。

        此外,您应该在 viewDidLoad 中设置标签,除非您有某种理由在每次按下 alertView 按钮时不断重置它们的值。

        或者,更简单的方法是 Anand K 所说的。

        -斯蒂芬

        【讨论】:

          【解决方案5】:

          您的代码是多余的,无论如何都没有意义.. 在这种情况下,您不需要使用 UIAlertView 作为属性。 使用这个:

          -(void)viewDidLoad{
              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login Error"
                                                                   message:@"Your login credentials     do not match"
                                                                  delegate:self
                                                         cancelButtonTitle:@"Try Again"
                                                         otherButtonTitles: @"Forgot Password",nil];
              alertView.tag = 1;
              [alertView show];
          }
          
          - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
          
              if (buttonIndex == 1) {
                  if(alertView.tag == 1)
                  {
          
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Forgot Password"
                                                                      message:@"Email"                                                                                                            delegate:self
                                                            cancelButtonTitle:@"Cancel"
                                                            otherButtonTitles:@"Reset Password", nil];
                      [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
                      alertView.tag = 2;
          
                      [alertView show];
          
                      NSLog(@"RESET");
          
                  }
              } else if (alertView.tag == 2) {
                 NSLog(@"Password");
              }
          }
          

          这段代码是干净的使用它;)

          【讨论】:

          • 你为什么复制邓肯的答案?
          • 我没有复制它..我的代码更干净..我不使用 UIAlertView 作为属性...此外,在 Duncun 的响应中有 2 次“buttonIndex == 1”。 .我的代码更有效率..你有 48k 点..也许你可以很容易地看到重要的区别..
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-09
          • 2019-06-28
          • 1970-01-01
          • 1970-01-01
          • 2013-11-30
          相关资源
          最近更新 更多