iosiphonedelegatesuialertviewuialertviewdelegate

Several UIAlertViews for a delegate


Currently I've got a class popping up UIAlertViews here and there. Currently, the same class is the delegate for these (it's very logical that it would be). Unfortunately, these UIAlertViews will call the same delegate methods of the class. Now, the question is - how do you know from which alert view a delegate method is invoked? I was thinking of just checking the title of the alert view, but that isn't so elegant. What's the most elegant way to handle several UIAlertViews?


Solution

  • Tag the UIAlertViews like this:

    #define kAlertViewOne 1
    #define kAlertViewTwo 2
    
    UIAlertView *alertView1 = [[UIAlertView alloc] init...
    alertView1.tag = kAlertViewOne;
    
    UIAlertView *alertView2 = [[UIAlertView alloc] init...
    alertView2.tag = kAlertViewTwo;
    

    and then differentiate between them in the delegate methods using these tags:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if(alertView.tag == kAlertViewOne) {
            // ...
        } else if(alertView.tag == kAlertViewTwo) {
            // ...
        }
    }