dismissViewControllerAnimated在块中不起作用

Dan*_*iel 10 iphone uiviewcontroller ios uialertcontroller

我试着关闭UIViewController一个UIAlertController在显示.

这是我的代码:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title 
                                                                         message:msg
                                                                  preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Accept" 
                                                   style:UIAlertActionStyleDefault 
                                                 handler:^(UIAlertAction *action)
             {
                 [self dismissViewControllerAnimated:YES completion:nil];
             }];

[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:^{}];
Run Code Online (Sandbox Code Playgroud)

但是,self永远不会被解雇.谁知道如何解决这个问题?

UPDATE

如果我设置[self dismissViewControllerAnimated:YES completion:nil];在块之外,它可以工作.

die*_*ado 11

只是用 [super.navigationController popViewControllerAnimated:YES];


Dan*_*iel 7

万一有人遇到同样的问题.我推了UIViewController,我没有提出来presentViewController:animated:completion:.这就是为什么[self.navigationController popViewControllerAnimated:YES];应该使用它.

奇怪的是,[self dismissViewControllerAnimated:YES completion:nil];在街区之外工作并没有在里面,我没有解释这个......


Dan*_*erz 5

[self dismissViewControllerAnimated:YES completion:nil]将关闭当前显示的视图(即"self")正在显示的任何视图控制器.你想要做的是在呈现视图控制器上为"self" 运行相同的方法.即

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)