在特定按钮上单击显示多个警报控制器?

时间:2016-01-08 07:43:20

标签: ios objective-c xcode

我有一个标签,通过点击“”或“”按钮询问问题和用户需要回答。如果用户直接点击NEXT按钮,则会弹出警告Choose Yes or No如果用户选择是,那么他自己就会得到一个新的子问题,如果用户点击NEXT而没有回答子问题,则会弹出另一个alertView。在条件上,仅显示一个警报视图。帮助PLZ 继承人的代码

 -(IBAction)btnNextClicked:(id)sender {

    activeButton = sender;
    if (([_txtOwnerShipPercentage.text isEqualToString:@" "] || _txtOwnerShipPercentage.text==nil || _txtOwnerShipPercentage.text.length==0) && (activeButton.tag == 1)) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Select Ownership Percentage" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
       [alertController addAction:okAction];
       [self presentViewController:alertController animated:YES completion:nil];
    } else if (activeButton.tag == 0){
       UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Select 'YES' or 'No'" preferredStyle:UIAlertControllerStyleAlert];
       UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
      [alertController2 addAction:okAction];
      [self presentViewController:alertController2 animated:YES completion:nil];

   }
}

我将标签设置为YES和No Button ... 1&分别为2。 activebutton只是全局声明的UIButton

2 个答案:

答案 0 :(得分:4)

以下是根据您的要求在一个上下文中管理两个或多个警报控制器的代码。

    //declare first alert controller
    UIAlertController *alertviewcontroller1 = [UIAlertController alertControllerWithTitle:@"My Alert 1"
                                                                   message:@"This is an action sheet of alert 1"
                                                            preferredStyle:UIAlertControllerStyleActionSheet];

    //declare second alert controller
    UIAlertController *alertviewcontroller2 = [UIAlertController alertControllerWithTitle:@"My Alert 2"
                                                                                  message:@"This is an action sheet of alert 2"
                                                                           preferredStyle:UIAlertControllerStyleActionSheet];

    //declare first alert controller action with hendler
    UIAlertAction *firstActionOfAlert1 = [UIAlertAction actionWithTitle:@"YES"
                                                          style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

                                                              //Present second alert controller when user taps on 'YES' of first alert controller.
                                                              [self presentViewController:alertviewcontroller2 animated:YES completion:nil];
                                                          }];

    //declare second alert controller action with hendler
    UIAlertAction *firstActionOfAlert2 = [UIAlertAction actionWithTitle:@"YES"
                                                          style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {


                                                          }];

    //add both actions relatively
    [alertviewcontroller1 addAction:firstActionOfAlert1];
    [alertviewcontroller1 addAction:firstActionOfAlert2];

    //Present first alert controller
    [self presentViewController:alertviewcontroller1 animated:YES completion:nil];

答案 1 :(得分:0)

如果未完全显示先前警报或未在alertView中点击操作,则上述解决方案将无效。使用此方法,

if (self.presentedViewController & !self.presentedViewController.isBeingPresented) {
        [self.presentedViewController presentViewController:alert animated:YES completion:nil];
}