开发者

How can i distinguish which UIAlertView's button was clicked from multiple alertview

In my appln I m using multiple UIAlertView and to determine which between was clicked in alertview i use the method

- (void) alertView:(UIAlertVie开发者_运维技巧w *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{

}

but i need only for the one alertview but it was responding for all alertviews. how to restrict to only to one alertview or to alertview to which i need determine the button action


Common practice is to use a unique number for the tag property of each UIAlertView, and then check the tag in your delegate callbacks. An easy way to do this is with an enum:

 enum {
     kServiceErrorAlert = 1,
     kFailedToSaveAlert = 2
 };

 ...

 alertView.tag = kServiceErrorAlert;
 [alertView show];


If you can afford to run on 4.x only, you can use blocks and forget about delegates and tags:

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];

See LambdaAlert on GitHub.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜