【问题标题】:How to dismiss a presented view controller when using UIAlertViewController in objective c?在目标 c 中使用 UIAlertViewController 时如何关闭呈现的视图控制器?
【发布时间】:2021-01-01 03:50:42
【问题描述】:

我没有使用任何使用导航控制器来呈现视图控制器。在该视图控制器中,我使用 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,它只会关闭警报,而不是父视图控制器。那么如何关闭父视图控制器呢?注意:我没有使用任何导航控制器。

【问题讨论】:

  • @matt 感谢您的回复,但我可以使用 [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];代替?
  • @matt 你能确认一下吗?

标签: ios objective-c swift xcode uialertviewcontroller


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多