简体   繁体   中英

How do I present multiple UIAlertController one after another?

Inside of a forin loop, I need to present an UIAlertController and wait for user confirmation before presenting the next one. I presented them inside a forin loop, but only the first one appears(after confirmation, the others don't show up).Any help would be greatly appreciated.

You can use the UIAlertController delegate when a button is pressed you show the next alert.

Make a global alert index:

NSUInteger alertIndex = 0; 

Make a global NSArray with your alert details in a NSDictionary eg:

self.alerts = @[@{@"title":@"Alert", @"message":@"Message 1"}, @{@"title":@"Alert", @"message":@"Message 2"}];

Call your first alert with an index, eg:

...title:self.alerts[alertIndex][@"title"]...

and

...message:self.alerts[alertIndex][@"message"]...

In your alert controller delegate's didClickButtonAtIndex:

alertIndex++;
// call your next alert here, explained above.

This UIViewController extension does what you want:

The alerts queue up, fifo. Ie latter alers wait and don't show until user respond to former alerts.

I need to do something similar to display hints to users throughout my app. Just managed to cobble this together and it works quite well and is easy to adapt and add to ...

        /////
        // Alert Properties in @interface
        ////
        @property NSOperationQueue *queue;
        @property NSMutableArray* operationArray;
        @property int operationCounter;
        @property int operationTotal;
        /////
        // Alert Properties End
        ////




        /////////
        // Multi Alert Code Start
        ////////


        - (void) alertCodeWithTitleString: (NSString*) titlestring AndMessageString:(NSString*)messagestring {
        NSOperation *tempoperation = [NSBlockOperation blockOperationWithBlock: ^(void) {



            NSString* tutTitleString = titlestring;
            NSString* tutMessageString = messagestring;




            dispatch_async(dispatch_get_main_queue(), ^{
                UIAlertController * alert=   [UIAlertController
                                            alertControllerWithTitle:tutTitleString
                                            message:tutMessageString
                                            preferredStyle:UIAlertControllerStyleAlert];


                        UIAlertAction* dontshow = [UIAlertAction
                                actionWithTitle:@"Don't Show This Again"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {



                                        [alert dismissViewControllerAnimated:YES completion:nil];

                                        self.operationCounter++;

                                        [self alertOperationCallMethod2];


                                }];
                                UIAlertAction* done = [UIAlertAction
                                actionWithTitle:@"Close"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {

                                    [alert dismissViewControllerAnimated:YES completion:nil];
                                    self.operationCounter++;

                                    [self alertOperationCallMethod2];

                                }];

                        [alert addAction:done];
                        [alert addAction:dontshow];

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

            } ];


        self.operationTotal++;
        [self.operationArray addObject:tempoperation];

        }

        -(void) alertOperationCallMethod1 {

        self.operationCounter = 0;
        self.operationTotal = 0;


        self.queue = [[NSOperationQueue alloc] init];
        self.operationArray = [[NSMutableArray alloc] init];


            [self alertCodeWithTitleString:@"Title1" AndMessageString:@"Message1"];

            [self alertCodeWithTitleString:@"Title2" AndMessageString:@"Message2"];

            [self alertCodeWithTitleString:@"Title3" AndMessageString:@"Message3"];

            //  Just keep adding method calls here to add alerts

            [self alertOperationCallMethod2];




        }
        -(void) alertOperationCallMethod2 {

        if (self.operationCounter<self.operationTotal) {

        [self.queue addOperation:self.operationArray[self.operationCounter]];

        }

        }

        -(void)viewWillAppear:(BOOL)animated{
           [super viewWillAppear:animated];
           //something here

        [self alertOperationCallMethod1];

           }
        /////////
        // Multi Alert Code End
        ////////

        ----

All you need to do is add another method call in alertOperationCallMethod1

Multiple versions of alertCodeWith..... method should allow you to customise your alerts to suit.

Hope this helps someone : )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM