16

Im creating a view in Xcode 4.3 and im unsure how to specify multiple UIAlertView's that have their own buttons with separate actions. Currently, my alerts have their own buttons, but the same actions. Below is my code.

-(IBAction)altdev {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
[alert show];
}

-(IBAction)donate {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
         }
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
    }
}  

Thanks for any help!

DiscoveryOV
  • 1,045
  • 2
  • 9
  • 24

5 Answers5

63

There is a useful property tag for UIView(which UIAlertView subclass from). You can set different tag for each alert view.

UPDATE:

#define TAG_DEV 1
#define TAG_DONATE 2

- (IBAction)altdev {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
   alert.tag = TAG_DEV;
   [alert show];
}

- (IBAction)donate {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
    alert.tag = TAG_DONATE;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == TAG_DEV) { // handle the altdev
      ...
    } else if (alertView.tag == TAG_DONATE){ // handle the donate

    }
}
cxa
  • 4,238
  • 26
  • 40
  • I still don't really understand how I incorporate that. When I attempt to put that in I keep getting the Use of undeclared identifier errors no matter what I do. I would appreciate it if you would reply with the code I have shown and your edits. Thanks – DiscoveryOV Mar 05 '12 at 02:12
  • Awesome thanks, works great. I'm sure you can tell I'm not wonderfully experienced with this yet, and it's great there's people like you to help us out. – DiscoveryOV Mar 06 '12 at 22:06
  • Um, actually now I'm having a problem with the alert not canceling (just continues with the action for either button) when the user taps the cancel button. Any insight? – DiscoveryOV Mar 06 '12 at 23:01
  • I added another option but I like this one so upped it. Don't define a tag with 0 as this is default tag on all objects (unless you have a need for a default of course). I assume you fixed this by now with the cancel thing but it is because you still need to test the button. – Recycled Steel Jul 03 '13 at 13:02
8

easier & newer

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 1) {
        // first alert...
    } else  {
        // sec alert...
    }
}

all done!

nima sp
  • 158
  • 2
  • 9
1

If you find it dificult to use delegate methods to differently identifying alert view Then you can also use This Category class to use completion Block for each AlertView.

Alert_ActionSheetWithBlocks

For eg.

UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];

UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];

I hope this one is the more easiest way as compare to tag + delegate method..

GurPreet_Singh
  • 386
  • 2
  • 16
0

He's right but you need to add this:

-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
      ...
    } else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate

    }
}

if buttonIndex==1 then you're using the FIRST otherbutton. 0 would be for cancel. But just do nothing for 0

Jeff Stone
  • 319
  • 1
  • 4
  • 14
0

Or you could do this (check the title name), is just another option... Mind identically titled alerts though!

-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc] 

                      initWithTitle:@"titleOneGoesHere"
                      message:@"messageGoesHere"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"Continue", nil];
[alert show];
}

-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc] 

                      initWithTitle:@"titleTwoGoesHere"
                      message:@"messageGoesHere"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"Continue", nil];
[alert show];
}

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

if (buttonIndex == 1)
{
    if([[alertView title] isEqualToString:@"titleOneGoesHere"])
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
    }
    else
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
    }
}
Recycled Steel
  • 2,272
  • 3
  • 30
  • 35