Different actions on the buttons for viewing alerts, depending on the type of alert

I have 3 types of browsing in my applications. 'WonAlert' 'LostAlert' 'NagAlert'

I am implementing this to give them action.

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

This worked well when I only had “wonAlert” and “lostAlert”, they had a dismissal and a “learn more” button that brought them to Wikipedia, now I want a nag warning to send them to the app store.

how can I do this so that the method described above knows from which warning the answer appears, or something like that?

Cheers Sam

+3
source share
4 answers

, UIAlertViews , :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView == wonAlert) {
        //DO STUFF
    }
    else if (alertView == lostAlert) {
        //DO OTHER STUFF
    }
    else if (alertView == nagAlert) {
        //OPEN APP STORE
    }
}

, .

+6

<UIAlertViewDelegate>, UIAlertView:

@interface MyViewController : UIViewController <UIAlertViewDelegate> { ... }

:

- (void) alertView:(UIAlertView *)_actionSheet clickedButtonAtIndex:(NSInteger)_buttonIndex {
    if ([_actionSheet.title isEqualToString:@"titleOfMyAlertView"]) {
        if (_buttonIndex == 0) {
            // do stuff for first button
        } 
        else if (_buttonIndex == 1) {
            // do something for second button
        }
        // etc. if needed
    }
}

_actionSheet.title . , NSString NSLocalizedString(), , .

+1

: 2

-

, , :

alertName.tag = #; //ex: alertName.tag = 1 

Then, in the clickedButtonAtIndex method, you will need to add an “if” block for each alert, as shown in the following code:

if(alert.tag == 1)
{
    if (buttonIndex == 0)
    {
        //do stuff
    }
    else
    {
        //do other stuff
    }
}
if(alert.tag == 2)
///...etc
+1
source

I would do what Alex offers, but use the tag property for AlertView to determine which AlertView was used.

0
source

Source: https://habr.com/ru/post/1729317/


All Articles