多个UIAlertView;每个都有自己的按钮和动作

时间:2012-03-04 08:36:00

标签: xcode button uialertview ibaction

我在Xcode 4.3中创建一个视图,我不确定如何指定多个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"]];
    }
}  

感谢您的帮助!

5 个答案:

答案 0 :(得分:63)

tagUIView子类来自)有一个有用的属性UIAlertView。您可以为每个警报视图设置不同的标记。

更新:

#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

    }
}

答案 1 :(得分: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...
    }
}

全部完成!

答案 2 :(得分: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 }];

我希望这与标记+委托方法相比更简单。

答案 3 :(得分: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那么你正在使用FIRST其他按钮。 0将取消。但是只为0做什么

答案 4 :(得分: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"]];
    }
}