首页 > 解决方案 > 在目标 c 中使用 UIAlertViewController 时如何关闭呈现的视图控制器?

问题描述

我没有使用任何使用导航控制器来呈现视图控制器。在该视图控制器中,我使用 NSTimer 在特定时间后关闭视图控制器。我还使用 AlertViewController 来显示时间结束的警报。但在那之后,只有 AlertViewController 不会关闭实际视图控制器。代码如下:

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Run Loop" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            //button click event
                        }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    [alert addAction:ok];
    [self presentViewController:alert animated:YES completion:nil];
    [self stop];  ***************** **Executing this function**
   
}

在特定时间后执行的功能是:

-(void)stop{
  [self dismissViewControllerAnimated:YES completion:nil];
}

当我使用 presentViewController 呈现警报时。因此,如果您使用 self.dismissViewController,它只会关闭警报,而不是父视图控制器。那么如何关闭父视图控制器呢?注意:我没有使用任何导航控制器。

标签: iosobjective-cswiftxcodeuialertviewcontroller

解决方案


A)只需使用代码,我就可以关闭呈现的视图控制器:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 

B) 或者另一种选择可以是首先我们关闭视图控制器,然后在完成处理程序上我们可以呈现 UIAlertViewController。


推荐阅读