UIAlertViewDelegate and more Alert windows

时光怂恿深爱的人放手 提交于 2019-12-04 17:48:22

问题


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 behaviors for each of them. So how to check which alertView send message?


回答1:


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
     }
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4346418/uialertviewdelegate-and-more-alert-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!