2012-10-12 60 views
0

在我的應用程序中,我有4個UI按鈕。單擊顯示相同警報視圖的每個按鈕包含學習和播放選項,同時單擊警報視圖,要顯示不同視圖取決於uibutton選擇。這裏是我的alert view的代碼。請幫助我在點擊選項上執行不同的視圖。用於多按鈕,多視圖的相同UIalertview

-(IBAction)animals 
{ 
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];  
[alert1 show]; 
} 
-(IBAction)birds 
{ 
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
[alert2 show];  
} 
-(IBAction)direct 
{ 
UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
[alert3 show]; 
} 
-(IBAction)fruit 
{ 
UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
[alert4 show]; 
} 
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex]; 

if ([buttonTitle isEqualToString:@"LEARN"]) 
{ 


    animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil]; 
    [self.navigationController pushViewController:aview animated:YES]; 
} 
else if([buttonTitle isEqualToString:@"PLAY"]) 
{ 
    birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil]; 
    [self.navigationController pushViewController:bview animated:YES]; 
} 
else 
{ 
    return; 
} 
} 
+0

沒有你的代碼.... – IronManGill

+0

@Gill代碼更新 – iosdev

+0

但這裏你有不同的警報意見...請更清楚:) – IronManGill

回答

1
-(IBAction)animals 
{ 
    UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];  
    alert1.tag = 1; 
    [alert1 show]; 
} 
-(IBAction)birds 
{ 
    UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    alert2.tag = 2; 
    [alert2 show];  
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex]; 

    if(alertView.tag == 1) 
    { 
      //animal alert 
    } 
    else if(alertView.tag == 2) 
    { 
      //birds alert 
    } 
    // and so on...... 
} 
+0

感謝隊友:)其工作 – iosdev

1

您有UIAlertView的標籤屬性。這是一個整數。您應該爲您的標籤分配不同的值。

-(IBAction)direct 
{ 
    UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert3 setTag:3]; 
    [alert3 show]; 
} 

然後clickedButtonAtIndex委託方法你應該觀察你的當前警報視圖的標籤:

if(alertView.tag == 3) //etc ... 

我不知道,但如果你使用ARC如果沒有你必須添加一個自動釋放當你實例化你的警報視圖。

+0

如果點擊其他按鈕什麼是標籤值? – iosdev

+0

我只給你一個警報的例子。對其他警報應用相同的方法。使用公式:Alert_N * alert = ....; [alert setTag:N]; – Pierre

1

聲明你UIAlertView全球範圍內,那麼你可以用UIAlertView代表比較它們的物體,像這個 -

UIAlertView *alert1; 
UIAlertView *alert2; 

-(IBAction)animals 
{ 
    alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil];  
    [alert1 show]; 
} 

-(IBAction)birds 
{ 
    alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert2 show];  
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(alertView ==alert1) 
    { 
    if (buttonIndex==1) 
    { 

    } 
    if(buttonIndex == 2) 
    { 

    } 
}  

if(alertView == alert2) 
{ 
    if (buttonIndex==1) 
    { 


    } 
} 

根據這一點,你就不需要標籤:)只需使用條件

+0

不,你不能使用它,因爲alert1不再被引用。 – Pierre

+0

現在我編輯了我的答案,我忘了說,警戒視圖是全球性的,不是本地的.... :) – IronManGill

+0

我希望你使用@property來聲明它們;) – Pierre

0
-(IBAction)animals 
{ 
    UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert1 show]; 
    [alert1 setTag:101]; 
} 
-(IBAction)birds 
{ 
    UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert2 show]; 
    [alert2 setTag:102]; 
} 


-(IBAction)direct 
{ 
    UIAlertView *alert3=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert3 show]; 
    [alert3 setTag:103]; 
} 


-(IBAction)fruit 
{ 
    UIAlertView *alert4=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"LEARN",@"PLAY",@"CANCEL",nil]; 
    [alert4 show]; 
    [alert4 setTag:104]; 
} 


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

    // learn has indextag = 0 ,play =1 cancel = 2 

    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex]; 

    if (alertView.tag == 101) { 



    switch (buttonIndex) { 
     case 0: 
      animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil]; 
      [self.navigationController pushViewController:aview animated:YES]; 

      break; 
     case 1: 
      birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil]; 
      [self.navigationController pushViewController:bview animated:YES]; 

      break; 

     case 2: 
      return; 
      break; 



     default: 
      break; 
    } 

     } 

    if (alertView.tag == 102) { 



     switch (buttonIndex) { 
      case 0: 
       animallearn *aview=[[animallearn alloc]initWithNibName:@"animallearn" bundle:nil]; 
       [self.navigationController pushViewController:aview animated:YES]; 

       break; 
      case 1: 
       birdslearn *bview=[[birdslearn alloc]initWithNibName:@"birdslearn" bundle:nil]; 
       [self.navigationController pushViewController:bview animated:YES]; 

       break; 

      case 2: 
       return; 
       break; 



      default: 
       break; 
     } 

    } 

    // perform the same for tag = 103 and 104 
} 
相關問題