开发者

UIAlertViewDelegate and more Alert windows

开发者 https://www.devze.com 2023-01-28 21:47 出处:网络
I have controller which implements UIAlertViewDelegate. In implementation I have: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

I have controller which implements UIAlertViewDelegate. In implementation I have:

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

method. When I create UIAlertView I put for 'delegate' to 'self' and it works fine. But problem is that now I have one more alert views and I want different behav开发者_StackOverflowiors for each of them. So how to check which alertView send message?


UIAlertView is a UIView subsclass and so has tag property you can use to differentiate between them:

UIAlertView *alert1 = ... //Create alert
alert1.tag = kActionTag1;
//show alert

...

UIAlertView *alert2 = ... //Create alert
alert2.tag = kActionTag2;
//show alert

And then in delegate method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
     if (alertView.tag == kActionTag1){
          // Perform 1st action
     }
     if (alertView.tag == kActionTag1){
          // Perform 2nd action
     }
}


The pointer to each specific alert view is sent in the alertView parameter of the delegate method. You simply need to track the pointers (for example through instance variables) so you know which is which and act accordingly.


UIAlertView gas a tag property. Set it when you create it and you can check for the tag in the delegate.

0

精彩评论

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