dismissViewControllerAnimated在块内不起作用

在显示UIAlertController后,我尝试closures一个UIViewController

这是我的代码:

 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:^{}]; 

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

UPDATE

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

只需使用[super.navigationController popViewControllerAnimated:YES];

如果有人有同样的问题。 我推UIViewController ,我没有presentViewController:animated:completion:它与presentViewController:animated:completion: 。 这就是为什么[self.navigationController popViewControllerAnimated:YES]; 应该用来代替。

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

[self dismissViewControllerAnimated:YES completion:nil]将closures当前正在显示的视图 (即“self”)正在显示的视图控制器。 你想要做的是在呈现视图控制器“自我”运行相同的方法。 即

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

你是否检查过哪个线程被调用? 如果它不是线程1,那么它不会正确地解除你的视图,因为UI操作只能在线程1上完成。 尝试创build一个方法解散,然后在主线程上调用它:

  ...handler { [self performSelectorOnMainThread:@selector(dismissModalView) withObject:nil waitUntilDone:NO]; }]; -(void)dismissModalView { [self dismissViewControllerAnimated:YES completion:nil]; }