菜鸟教程小白 发表于 2022-12-11 19:31:54

ios - 如何一次关闭两个模态视图 Controller ?


                                            <p><p>我有一个 ViewControllerA,它位于导航堆栈中。
A 模态地呈现另一个 ControllerB,而 B 又可以模态地呈现另一个 ControllerC。</p>

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

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

<pre><code>    let p = self.presentingViewController
    self.dismiss(animated: true) {
      p?.dismiss(animated: true, completion: nil)
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使最顶层的 ViewController 成为一个属性。确保 <code>B</code> 为 <code>C</code> 设置最顶层的 ViewController 。</p>

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

}
</code></pre>

<p>这为您提供了很大的灵 active 来决定哪个 ViewController 执行解除操作。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何一次关闭两个模态视图 Controller ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48027052/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48027052/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何一次关闭两个模态视图 Controller ?