2 UIAlertView with 3 buttons?

3.4k Views Asked by At

I wanna know how can I make 2 UIAlertView, with 3 buttons, the UIAlertViews(2) needs be different, the options and the actions... how ???

3

There are 3 best solutions below

0
On BEST ANSWER

Try this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Message." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Button 2", @"Button 3", nil];
alert.tag = 1;               
[alert show];

then do the same for the next alertView, just change the tag to 2

Then just run this method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
     if(alert.tag == 1) {
          //the alert tag 1 was just closed - do something
     }
}

Also - make sure you include the UIAlertViewDelegate

0
On

Just check 2 in the alertviews(4) delegate method which alertviews was responsible for the method to be called(1).

0
On

UIAlertview delegates are deprecated in ios 9.0

Also when you add more then 2 buttons, they will be vertically assigned by IOS.

you can do with simply UIAlertController

UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:[[[NSBundle mainBundle] infoDictionary]
                                                            objectForKey:@"CFBundleDisplayName"]
                                  message:@"share via"
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* fbButton = [UIAlertAction
                                actionWithTitle:@"Facebook"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    // Add your code
                                }];
    UIAlertAction* twitterButton = [UIAlertAction
                                   actionWithTitle:@"Twitter"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action)
                                   {
                                       // Add your code

                                   }];
    UIAlertAction* watsappButton = [UIAlertAction
                                    actionWithTitle:@"Whatsapp"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                      // Add your code
                                    }];
    UIAlertAction* emailButton = [UIAlertAction
                                    actionWithTitle:@"Email"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {

                                        // Add your code
                                    }];
    UIAlertAction* cancelButton = [UIAlertAction
                                  actionWithTitle:@"Cancel"
                                  style:UIAlertActionStyleDefault
                                  handler:^(UIAlertAction * action)
                                  {
                                      //Handel no, thanks button

                                  }];

    [alert addAction:fbButton];
    [alert addAction:twitterButton];
    [alert addAction:watsappButton];
    [alert addAction:emailButton];
    [alert addAction:cancelButton];

    [self presentViewController:alert animated:YES completion:nil];

IOS 9 Alert with vertical buttons