gpt4 book ai didi

ios - 一旦下一个以模态方式出现,就关闭或删除以前以模态方式呈现的 View Controller

转载 作者:可可西里 更新时间:2023-10-31 23:56:52 32 4
gpt4 key购买 nike

我的目标包括很多 View ,需要根据每个用户操作以模态方式呈现不同的 View 。这里我想做的是获得更清晰的 View 层次结构和更好的用户体验。

  1. Root View Controller present First View Controller modally
  2. When I clicked button on the First View Controller, then the Second View Controller appear modally over it.
  3. As soon as the Second View Controller did appear, I want to dismiss or remove the first one from view hierarchy.

我可以吗? 如果是,我应该怎么做?

如果不是,解决这个问题的正确方法是什么,因为我将在每个 View 上呈现许多模态呈现的 View Controller 。我认为即使我想关闭当前 View ,当当前 View 关闭时,前一个 View 仍会出现。

更新:

VC1 (Root) > VC 2 (which was present modally) > VC 3 (which was present modally over VC 2)

当我关闭 VC3 时,VC2 仍在 View 内存中。因此,我不想在关闭 VC3 后立即显示 VC2,而是希望通过删除或关闭 来查看 VC1 >VC2 来自 View 层次结构。

enter image description here

想要 :在图像中,当我消除蓝色时,我不想在我的 View 内存中看到粉红色,我想在蓝色出现时立即将其删除.

这就是我想做的。

有什么帮助吗?谢谢。

最佳答案

那么,让我们假设您有一个类似于以下内容的 Storyboard:

enter image description here

应该发生的是:

  • 呈现第二个 ViewController(来自第一个 ViewController)。
  • 呈现第三个 ViewController(来自第二个 ViewController)。
  • 解散到第一个 ViewController(从第三个 ViewController)。

在第三个 ViewController 按钮的 Action 中:

@IBAction func tapped(_ sender: Any) {
presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

如您所见,通过访问 presentingViewController在当前 ViewController 中,您可以取消 View Controller 的先前层次结构:

The view controller that presented this view controller.

通过实现 presentingViewController?.presentingViewController? 这意味着:呈现当前 ViewController :)

这可能看起来有点困惑,但它非常简单。

所以输出应该是这样的(我将背景颜色添加到 viewControllers - 如 vc1:橙色,vc2:黑色和 vc3:浅橙色 - 使其显示清晰):

enter image description here

编辑:

如果您要求删除中间的 ViewController(在本例中为第二个 ViewController),dismiss(animated:completion:)自动执行此操作:

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. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

指的是你在问什么:

I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.

我认为这在 UI 上显示得很清楚(而且我觉得没问题),但如 dismiss 文档讨论中所述,第三个和第二个都将从堆栈中删除。这是正确的方法。

关于ios - 一旦下一个以模态方式出现,就关闭或删除以前以模态方式呈现的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327216/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com