Reputation: 3588
In a same view controller, I have to show different alerts with different actions triggered by the alert buttons (this view controller is the delegate of the alerts).
Is there a way to reuse the alert code init/show/release, considering that in
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
I need the alerts to be distinguishable.
Upvotes: 2
Views: 1912
Reputation: 3655
You can define a set of constants to represent each different type of alert view you're managing. For instance:
enum {
MyFirstTypeOfWarning,
MySecondTypeOfWarning
};
typedef NSInteger SPAlertViewIdentifier;
Then, whenever you find yourself needing to present a UIAlertView, call a method that wraps up the init/show show code and sets the tag of the UIAlertView:
- (void)initializeAndPresentUIAlertViewForWarningType:(SPAlertViewIdentifier)tag {
// Standard alloc/init stuff
[alertView setTag:tag];
[alertView show];
}
Then, in alertView:clickedButtonAtIndex: you can check the tag of the alert view passed in and respond accordingly.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([alertView tag] == MyFirstTypeOfWarning) {
// Process button index for first type of alert.
} ...
}
Upvotes: 7
Reputation: 6132
Exactly what was stated by 7KV7. You need to tag the alert view, and check in your event handler what the sender's tag was. This way, you can create different actions for different alert views in the same eventhandler (clickedButtonAtIndex).
Upvotes: 2
Reputation: 26400
You can get the alert view here itself
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView isEqualTo:yourAlertView]) // you can try isEqual:
{
//Do something
}
//Another option is set tags to alertviews and check these tags
// if(alertView.tag == yourAlertView.tag)
//{
//Do something
//}
}
Upvotes: 4