【问题标题】:How do I present multiple UIAlertController one after another?如何一个接一个地呈现多个 UIAlertController?
【发布时间】:2016-01-31 09:09:03
【问题描述】:

在 forin 循环中,我需要呈现一个 UIAlertController 并等待用户确认,然后再呈现下一个。我在一个forin循环中展示了它们,但只出现了第一个(确认后,其他的没有出现)。任何帮助将不胜感激。

【问题讨论】:

  • 你为什么要放入 for 循环?正如您所说,您必须等待用户确认,因此在 Alertcontroller 的按钮操作上创建一个新警报,
  • 问题是每个警报在用户确认时执行一个任务(UIAlertControllerStyleActionSheet 有多个选项)。我正在尝试使用用户选择的不同选项对多个对象执行任务。
  • 这真的是最好的用户体验吗?正如您在下面的评论中所说,如果您有 100 个对象怎么办?你真的要让用户确认 100 个单独的警报吗?那会很糟糕。
  • @rmaddy 这只是通常不会发生的最坏情况的一个例子。我要做的是让每个对象执行一个操作,具体取决于在 UIAlertController 中使用操作表样式选择了哪个选项。我在 uitableview 中选择对象并将它们放入数组中。有没有更好的方法来循环遍历数组并为每个对象提供用户多项选择?

标签: ios objective-c uialertcontroller for-in-loop


【解决方案1】:

您可以在按下按钮时使用 UIAlertController 委托来显示下一个警报。

制作全局警报索引:

NSUInteger alertIndex = 0; 

在 NSDictionary 中使用您的警报详细信息创建一个全局 NSArray,例如:

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

使用索引调用您的第一个警报,例如:

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

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

在您的警报控制器委托的 didClickButtonAtIndex 中:

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

【讨论】:

  • 如果我有 100 个对象会怎样?我正在为我的 UIAlertController 使用 UIAlertControllerSytleActionSheet,每个对象都可以显示不同的选项供用户选择。
  • 不要让任何东西成为全球性的。也许您的意思是实例变量或属性?
  • 这是可能的,只是会更复杂,您需要以不同的方式处理每一个。我以前从未见过这样的东西,怀疑Apple会批准..为什么不制作带有20个分段按钮的5个视图控制器或那一行?是的@rmaddy,您可以根据需要进行编辑:)
【解决方案2】:

我需要做一些类似的事情来在我的应用中向用户显示提示。 只是设法将它拼凑在一起,效果很好,很容易适应和添加......

        /////
        // 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
        ////////

        ----

您需要做的就是在 alertOperationCallMethod1 中添加另一个方法调用

alertCodeWith..... 方法的多个版本应该允许您自定义警报以适应。

希望这对某人有帮助:)

【讨论】:

    【解决方案3】:

    This UIViewController 扩展做你想做的事:

    警报排队,先进先出。即后面的警报等待并且不显示,直到 用户响应以前的警报。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 2020-01-04
      • 1970-01-01
      • 2015-10-10
      • 2020-10-19
      • 1970-01-01
      • 2020-08-16
      相关资源
      最近更新 更多