【问题标题】:popViewController:animated: not working in iOS 9popViewController:animated: 在 iOS 9 中不工作
【发布时间】:2015-09-19 07:40:12
【问题描述】:

我正在使用子类 UINavigationController 来管理我的应用程序中的所有视图控制器。它在主流程中推送和弹出视图控制器,并以模态方式呈现和关闭那些任意需要的视图控制器。

在一种情况下,我需要在主流程中弹出另一个视图控制器之前以模态方式呈现一个视图控制器,如下所示:

//Called in custom UINavigationController subclass
[self presentViewController:searchVC animated:YES completion:^{
    [self popViewControllerAnimated:NO]; 
}];

上面的代码在 iOS 8 之前可以顺利运行,但在 iOS 9 中无法运行。当关闭呈现的 vc 时,仍然保留与以前相同的 viewController。

此外,这正在控制台中记录:

popViewControllerAnimated: called on <CustomNavigationController 0x7d846600> while an existing transition or presentation is occurring; the navigation stack will not be updated.

到目前为止,这从来都不是问题,尤其是在完成块中调用了 popViewController 方法。

这可能是一个错误吗?

欢迎任何解决方案/建议/解决方法。

【问题讨论】:

    标签: ios objective-c uiviewcontroller uinavigationcontroller ios9


    【解决方案1】:

    将 popViewController 调用包装在 dispatch_async 块中有效。

    [self presentViewController:searchVC animated:YES completion:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self popViewControllerAnimated:YES];
        }); 
    }];
    

    【讨论】:

      【解决方案2】:

      它在标准 UINavigationController 中对我有用,即使您收到“开始/结束外观转换的不平衡调用”警告。以下替换 popViewControllerAnimated: 的代码消除了该警告。

      NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
      [viewControllers removeLastObject];
      self.navigationController.viewControllers = [viewControllers copy];
      

      所以我猜问题出在您的子类中。你覆盖presentViewController:animated:completion: 还是popViewControllerAnimated:

      【讨论】:

      • 不,我不会覆盖这些方法。我能够通过在 dispatch_async 块中调用 popViewController 来解决问题。但是,现在会记录一条带有“开始/结束外观转换的不平衡调用”的消息
      • 我在答案中发布的代码应该可以解决这个问题。它也可能不需要dispatch_async 包装器。
      • 这样做似乎非常不安全。
      【解决方案3】:

      是的,我在 iOS 9 中也注意到了这个问题。 我将代码更改为(Swift):

      controller.dismissViewControllerAnimated(true, completion: nil)
      

      其中“控制器”是呈现的 VC 实例。

      【讨论】:

        【解决方案4】:

        如果您使用的是拆分视图控制器,请尝试将其移除。

        【讨论】:

          猜你喜欢
          • 2015-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-24
          • 2015-12-15
          • 2018-12-06
          相关资源
          最近更新 更多