开发者

2 UIAlertView with 3 buttons?

开发者 https://www.devze.com 2023-02-11 03:18 出处:网络
I wanna know how can I make 2 UIAlertView, with 3 buttons, the UIAlertViews(2) needs be different, the options and the a开发者_C百科ctions... how ???Try this:

I wanna know how can I make 2 UIAlertView, with 3 buttons, the UIAlertViews(2) needs be different, the options and the a开发者_C百科ctions... how ???


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


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


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];

2 UIAlertView with 3 buttons?

0

精彩评论

暂无评论...
验证码 换一张
取 消