问题
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