2013-11-15 48 views
0

我有这个UIAlertview,我试图用它来调用一个函数。显示了fogotpassword alert视图,然后重置alertview出现,但是当我试图调用NSLog(@“Password”)时,通过按下第一个按钮重置alertview它不会被调用。相反,重置的alertview按钮会再次弹出。我将不胜感激任何帮助。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 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"); 
    } 
} 

回答

3

你的逻辑全乱了。你设置了两个标签,这两个标签总是成立的。由于您似乎有两个不同的警报视图的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 
     } 
    } 
} 
+0

谢谢,只要时限到了,你会得到一个复选标记。我只是按照另一个stackoverflow的答案,我想这不是最好的方法 – user2997441

+0

请记住,使用标签而不是ivars的警报视图也非常好。您只需在创建警报视图时设置标记,而不是在委托方法中。 – rmaddy

1

无需使用标签

尝试以这种方式

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

您的代码很混乱。您创建一个警告视图“忘记密码”并显示它,而不设置它的标签。然后,在您的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"); 
} 

}

0

您应该使用alertView.tag == 1和alertView.tag == 2在你的方法。正如你现在所做的那样,既然你明确地设置了忘记密码.tag = 1和resetPassword.tag = 2,那么只要buttonIndex == 1,if语句总是正确的,然后检查每个项目。

另外,您应该在viewDidLoad中设置标签,除非您有理由在每次按下alertView按钮时都保持重置其值。或者,更简单的方法就是Anand K所说的。

-Stephen

0

你的代码是多余的,在任何情况下也没有感.. 你不需要在这种情况下使用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"); 
    } 
} 

此代码是干净的使用它;)

+0

你为什么重复Duncan的答案? – rmaddy

+0

我没有复制它..我的代码更干净..我不使用UIAlertView属性...此外在Duncun的响应有2次“buttonIndex == 1”..我的代码更有效。你有48k分..也许你可以很容易地看到重要的区别.. –