2011-08-25 64 views
0

我有2個警報在同一頁上。問題在於clickedButtonAtIndex正在回答兩個警報,而不僅僅是第二個警報。我如何區分它們?整理2 UIalertview的按鈕點擊

UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"\n\n\n" 
                 delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK",nil), nil]; 

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct phone no." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Try Again", nil]; 

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

在第一個輸入電話號碼,然後點擊確定。如果驗證,很好。但是,如果它不顯示第二個警報。現在,當他們點擊「再試一次」時,clickedButtonAtIndex就會在第一次和第二次提醒時運行。我需要它只能在第二次運行。

回答

0

,你所要做的是標籤爲給不同的標籤既警報視圖,然後訪問: -

UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"\n\n\n" 
                 delegate:self cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"OK",nil), nil]; 
passwordAlert.tag=1; 

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct phone no." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Try Again", nil]; 
alert.tag=2; 

- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if(alert.tag==1) 
    { 
     if (buttonIndex==0) { 

     } 
     else if (buttonIndex==1) { 

     } 
     } 
if(alert.tag==2) 
    { 
     if (buttonIndex==0) { 

     } 
     else if (buttonIndex==1) { 

     } 
} 
+0

真棒。謝謝我現在還沒有用到蘋果文檔。謝謝大家花時間回答。 – Rick

0

嘗試爲每個UIAlertView添加一個標記,然後在alertView中使用該標記:clieckedButtonAtIndex來確定哪個UIAlertView處於活動狀態。

查看詳細信息和示例代碼,在此之前的問題:Several UIAlertViews for a delegate