2014-03-05 20 views
0

我在方法中有2個alertView,我想檢查第二個alertView的按鈕索引。我該怎麼做?我想獲取else條件的buttonIndex。以下是我正在嘗試的代碼。要做到這一點檢查第二個UIAlertView的buttonIndex

-(IBAction)buttonClick{ 

    NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.xyz.com"] encoding:NO error:nil]; 

    if(connect == NULL) {   
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:@"Please check your internet connection" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];   
     [alert show];    
    } else {  
     UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"Upload to the server?" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"CANCEL",nil];  
     [alert1 show];   
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     My functionality 
} 

回答

4

一種方法是具有警報視圖的情況下,全球和檢查那些在alertview的委託方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    // assuming the myAlert2 is your alert's instance 
    if (alertView == myAlert2) 
    { 
     if (buttonIndex == 0) 
     { 
      ... 
     } 
    } 
} 

或者你也可以給標籤您的警報,然後檢查標籤該alertView的委託方法,如:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 2) 
    { 
     if (buttonIndex == 0) 
     { 
      ... 
     } 
    } 
} 
+1

我會說這是「存儲alertViews的財產」,而不是「讓他們的全球」更好的建議。否則同意:) – JoeFryer

1

您可以使用標籤警報視圖

如:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"hai" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; 

alert.tag = 1; 

[alert show]; 

,那麼你可以檢查像下面

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 1) 
    { 
     if (buttonIndex == 0) 
     { 
      ... 
     } 
    } 
} 
相關問題