【问题标题】:Present View Controller right after Dismissing View Controller with no animation在没有动画的情况下关闭视图控制器后立即呈现视图控制器
【发布时间】:2015-09-25 22:43:03
【问题描述】:

我在屏幕上显示了一个UIImagePickerController。当我选择照片时,我需要关闭PickerController,然后立即展示另一张PhotoEditController。这是我的代码

picker.dismissViewControllerAnimated(false, completion: {
    self.presentViewController(editPhotoVC, animated: false, completion: nil)
})

在关闭旧 VC 和呈现新 VC 之间有 0.1 秒的闪烁,因此显示了 presentingViewController (self)。我如何在一个优雅的解决方案中避免这种情况而不是破解它?谢谢

【问题讨论】:

  • 既然你没有为任何东西设置动画,为什么不直接展示视图控制器而不关闭前一个呢? If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack.
  • 怎么样?苹果不允许这样做
  • 对不起,我的意思是在已经呈现的视图控制器(选择器)上呈现你的新视图控制器。然后,当您想要全部关闭它们时,请从呈现第一个视图控制器的原始视图控制器中关闭。
  • picker.dismissViewControllerAnimated 对吗?不应该只是dismissViewControllerAnimated吗?
  • @LLooggaann 我明白你的意思。这实际上是我实施的。我在 self 上实现了来自 editPhotoVC 的委托回调,以将两个视图控制器一起解除。你应该写一个答案,我会标记为已回答。

标签: ios iphone swift presentviewcontroller


【解决方案1】:

标准的实现方式是用动画关闭第一个视图控制器 (VC),然后用动画呈现第二个 VC。

但是,根据您的视图层次结构,您也可以先加载第二个 VC,然后在其上显示第一个 VC。有了这个简单地关闭没有动画的第一个 VC 应该立即显示在第二个 VC 下方。

第三种方法,正如 LLooggaann 所建议的那样,不要忽略第一个 VC,而只是提出第二个 VC。完成后,一次性关闭整个视图控制器层次结构。

【讨论】:

    【解决方案2】:

    您可以实例化PhotoEditController 的实例,然后执行以下操作:

    instance.willMoveToParentViewController(controllerThatPresentsPicker)
    controllerThatPresentsPicker.addChildViewController(instance)
    controllerThatPresentsPicker.view.insertSubview(instance.view, belowSubview: picker.view)
    instance.didMoveToParentViewController(controllerThatPresentsPicker)
    

    虽然看起来有点像黑客

    【讨论】:

      【解决方案3】:

      这很可能是因为关闭被发送到呈现选择器的 VC,它关闭选择器模式 vc 并在尝试呈现本身的父 VC 后执行完成处理程序。 一种解决方案是创建自己的转换类,您基本上可以从here 复制粘贴代码来做到这一点

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 2011-04-24
        • 1970-01-01
        • 2017-04-07
        相关资源
        最近更新 更多