我在导航堆栈中有一个视图控制器A。
一个模态表示另一个控制器B,而后者又可以模态表示另一个控制器C。

当用户在C中按下按钮时,我想同时关闭C和B以返回到A。
我如何一次解雇B和C?

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

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

最佳答案

将最顶部的视图控制器设置为属性。确保BC设置了最顶层的视图控制器。

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)
    }

}

这为您消除了哪个视图控制器提供了很大的灵活性。

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

10-11 22:15
查看更多