【问题标题】:dismissViewControllerAnimated does not work within a blockdismissViewControllerAnimated 在块内不起作用
【发布时间】:2015-03-20 03:18:04
【问题描述】:

在显示UIAlertController 后,我尝试关闭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 永远不会被解雇。有谁知道如何解决这个问题?

更新

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

【问题讨论】:

    标签: ios iphone uiviewcontroller uialertcontroller


    【解决方案1】:

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

    【讨论】:

    • 是的...这是我 4 个月前写的:D
    【解决方案2】:

    如果有人遇到同样的问题。我推了UIViewController,我没有用presentViewController:animated:completion: 呈现它。这就是为什么应该改用 [self.navigationController popViewControllerAnimated:YES]; 的原因。

    奇怪的是[self dismissViewControllerAnimated:YES completion:nil];在街区外工作而不在里面,我对此没有任何解释......

    【讨论】:

      【解决方案3】:

      [self dismissViewControllerAnimated:YES completion:nil] 将关闭当前显示视图(即“self”)正在显示的所有视图控制器。您要做的是在 presenting 视图控制器上为“self”运行相同的方法。即

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

      【讨论】:

      • 但 self(当前的 UIViewController)是我想要关闭的视图。我尝试添加.parentViewController,它没有任何区别。
      • 是的,没错。你想解雇“自我”。 “self.presentingViewController”可能会这样做。更新答案。
      • 在调试器中,“self.presentingViewController”和“self.parentViewController”都是nil吗?
      • 不,self.parentViewController 的类型是 XXXRootNavigationController
      • self.presentingViewController 怎么样?
      【解决方案4】:

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

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

      【讨论】:

      • 是的,试过了,没用。顺便说一句,最短的选择器是performSelectorOnMainThread:withObject:waitUntilDone:
      猜你喜欢
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多