Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different alert views and viewcontroller

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.

like image 783
Sefran2 Avatar asked Jul 27 '26 22:07

Sefran2


2 Answers

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.
    } ...
}
like image 88
Stephen Poletto Avatar answered Jul 30 '26 12:07

Stephen Poletto


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
//}
    }
like image 21
visakh7 Avatar answered Jul 30 '26 10:07

visakh7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!