OGeek|极客世界-中国程序员成长平台

标题: ios - 如何一次关闭两个模态视图 Controller ? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:31
标题: ios - 如何一次关闭两个模态视图 Controller ?

我有一个 View Controller A,它位于导航堆栈中。 A 模态地呈现另一个 Controller B,而 B 又可以模态地呈现另一个 Controller C。

当用户点击 C 中的按钮时,我想关闭 C 和 B 以返回 A。 如何同时解雇 B 和 C?

下面的代码有效,但这样做安全吗?

    let p = self.presentingViewController
    self.dismiss(animated: true) {
        p?.dismiss(animated: true, completion: nil)
    }



Best Answer-推荐答案


使最顶层的 View Controller 成为一个属性。确保 BC 设置最顶层的 View Controller 。

class C: UIViewController {

    var topmostViewController: UIViewController?

    @IBAction func dismiss(_ sender: Any) {
        // If topmostViewController was not set, then assume
        // the presenting view controller is to be dismissed.
        (topmostViewController ?? presentingViewController)?.dismiss(animated: true)
    }

}

这为您提供了很大的灵 active 来决定哪个 View Controller 执行解除操作。

关于ios - 如何一次关闭两个模态视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027052/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4