我在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"]];
    }
}


谢谢你的帮助!

最佳答案

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

    }
}

10-07 19:41
查看更多