2012-03-04 78 views
16

我在Xcode 4.3中創建一個視圖,並且我不確定如何指定具有各自按鈕的多個UIAlertView,並使用單獨的操作。目前,我的提醒有自己的按鈕,但操作相同。以下是我的代碼。多個UIAlertView;每個都有自己的按鈕和動作

-(IBAction)altdev { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

-(IBAction)donate { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    [alert show]; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; 
     } 
} 


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 1) { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; 
    } 
} 

感謝您的幫助!

回答

63

有一個有用的屬性tagUIView(其中UIAlertView從子類)。您可以爲每個警報視圖設置不同的標籤。

UPDATE:

#define TAG_DEV 1 
#define TAG_DONATE 2 

- (IBAction)altdev { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    alert.tag = TAG_DEV; 
    [alert show]; 
} 

- (IBAction)donate { 
    UIAlertView *alert = [[UIAlertView alloc] 

          initWithTitle:@"titleGoesHere" 
          message:@"messageGoesHere" 
          delegate:self 
          cancelButtonTitle:@"Cancel" 
          otherButtonTitles:@"Continue", nil]; 
    alert.tag = TAG_DONATE; 
    [alert show]; 
} 

-(void)alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == TAG_DEV) { // handle the altdev 
     ... 
    } else if (alertView.tag == TAG_DONATE){ // handle the donate 

    } 
} 
+0

我還是真的不明白我是如何結合這一點。當我試圖將其放入時,無論我做什麼,我都一直在使用未聲明的標識符錯誤。 我將不勝感激,如果你會回覆我顯示的代碼和你的編輯。謝謝 – DiscoveryOV 2012-03-05 02:12:06

+0

已根據您的要求更新。 – cxa 2012-03-05 03:29:44

+0

非常感謝,效果很好。我敢肯定,你可以告訴我對此還沒有很好的經驗,而且有很多像你這樣的人來幫助我們。 – DiscoveryOV 2012-03-06 22:06:39

0

他是正確的,但你需要補充一點:

-(void)alertView:(UIAlertView *)alertView 
clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev 
     ... 
    } else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate 

    } 
} 

如果buttonIndex == 1,那麼您在使用第一otherbutton。 0將用於取消。但只是沒有爲0

0

或者你可以做到這一點(檢查標題名稱),只是另一種選擇...雖然心目中同樣標題警報!

-(IBAction)altdev { 
UIAlertView *alert = [[UIAlertView alloc] 

         initWithTitle:@"titleOneGoesHere" 
         message:@"messageGoesHere" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

-(IBAction)donate { 
UIAlertView *alert = [[UIAlertView alloc] 

         initWithTitle:@"titleTwoGoesHere" 
         message:@"messageGoesHere" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         otherButtonTitles:@"Continue", nil]; 
[alert show]; 
} 

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

if (buttonIndex == 1) 
{ 
    if([[alertView title] isEqualToString:@"titleOneGoesHere"]) 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]]; 
    } 
} 
8

容易&新

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 1; 

UIAlertView *alert = [[UIAlertView alloc] init... 
alert.tag = 2; 



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if(alertView.tag == 1) { 
     // first alert... 
    } else { 
     // sec alert... 
    } 
} 

全部完成!

1

如果您發現使用委託方法來區別識別警報視圖很困難,那麼您也可以使用此類別類爲每個AlertView使用完成塊。

Alert_ActionSheetWithBlocks

對於如。

UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }]; 

UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }]; 

我希望這一個是最簡單的多方式比較標記+委託方法..

相關問題