如何区分从多个uialertview点击的UIAlertView按钮

时间:2010-11-30 06:46:24

标签: iphone

在我的应用程序中,我使用多个UIAlertView并确定在alertview中点击了哪个之间我使用的方法

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

}

但我只需要一个alertview,但它正在响应所有的alertviews。如何限制只有一个alertview或alertview我需要确定按钮操作

2 个答案:

答案 0 :(得分:3)

通常的做法是为每个UIAlertView的{​​{3}}属性使用唯一编号,然后检查代理回调中的标记。一个简单的方法是使用枚举:

 enum {
     kServiceErrorAlert = 1,
     kFailedToSaveAlert = 2
 };

 ...

 alertView.tag = kServiceErrorAlert;
 [alertView show];

答案 1 :(得分:2)

如果你能负担得起只运行4.x,你可以使用积木而忘记代表和标签:

LambdaAlert *alert = [[LambdaAlert alloc]
    initWithTitle:@"Test Alert"
    message:@"See if the thing works."];
[alert addButtonWithTitle:@"Foo" block:^{ NSLog(@"Foo"); }];
[alert addButtonWithTitle:@"Bar" block:^{ NSLog(@"Bar"); }];
[alert addButtonWithTitle:@"Cancel" block:NULL];
[alert show];
[alert release];

请参阅LambdaAlert on GitHub

相关问题