【问题标题】:Show ViewController and dismiss first显示 ViewController 并首先关闭
【发布时间】:2017-01-07 15:41:01
【问题描述】:

我有一个视图控制器“A”,它以模态方式呈现另一个视图控制器 B。

在故事板中它看起来像这样:

UIViewController "A" -> UINavigationViewController "B" -> rootViewController" "C"

我的目标是呈现B并隐藏A,我该如何实现?

我尝试在 C 中设置属性并关闭 A,但它不起作用,因为我关闭了 C 并离开了 A。

AViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showSomething" {
            if let destination = segue.destination as? UINavigationController {
                if let viewController = destination.viewControllers.first as? CViewController {
                    viewController.delegate = self
                }
            }
        }
    }

CViewController

override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate.dismiss(animated: false)
    }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    关闭呈现的 View Controller 将 - 也 - 自动关闭其层次结构中呈现的 View Controller。

    为了更清楚,假设您有三个 ViewController:ABC,其中 A 是根 ViewController,它们看起来像:

    A => B => C
    

    假设当前呈现的 ViewController 是 B,-如果我猜对了-您想同时呈现 C 和关闭 B,正如我所提到的,关闭一个 ViewController(在这个例子中是B) 导致关闭其层次结构中的任何 ViewController(在本示例中为 C)。

    如果您想要执行以下行为,这可能不是您想要实现的目标:

    A 导航到BBC,当关闭C 时,我想转到A,而不查看B

    你需要让C ViewController 来做这个,通过实现:

    presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
    

    这会导致关闭当前呈现的 C(即 B)的 ViewController 的演示者并直接返回到 A

    有关这方面的更多信息,您可能需要查看this answer

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      你不能关闭 A,因为它正在呈现 C。你必须先关闭 A,然后使用呈现 A 的视图控制器来呈现 C。像这样:

      // call this from A
      dismiss(animated: false) {
          // completion closure
          self.presentingViewController.present(cViewController, animated: true)
      }
      

      通过我的手机回答,所以可能需要一些修复才能编译。

      【讨论】:

        猜你喜欢
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 2023-03-07
        相关资源
        最近更新 更多