Iphone – Different actions on alert views buttons, depending on the alert view

iphone

I have 3 alert views in my apps.
'wonAlert'
'lostAlert'
'nagAlert'

I implement this to give them actions.

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

This used to work fine, when I had just the 'wonAlert' and 'lostAlert', they had a dismiss and a learn more button that took them to wikipedia,
now I want the nag alert to take them to the app store.

how can I make it so the above method knows which alert view the tap is coming from, or something like that?

Cheers, Sam

Best Answer

It sounds like you've got the UIAlertViews in variables, so I'd use them:

- (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
    }
}

More than one view can have the same tag and you can easily mistype the title or change it and forget to update the delegate method.

Related Topic