2013-11-15 75 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分..也許你可以很容易地看到重要的區別.. –