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.
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.
} ...
}
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
//}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With