菜鸟教程小白 发表于 2022-12-11 17:58:31

ios - 如何从模态视图 Controller 将数据传递回 View 控件


                                            <p><p>所以通常我会为此使用委托(delegate)模式,但这是一个棘手的情况。</p>

<p> ViewControllerA 呈现 ->ViewControllerB 呈现 ->ViewControllerC。</p>

<p>当用户完成 ViewControllerC 中的步骤后,我将在一次调用中同时关闭 B 和 C</p>

<pre><code>   self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
</code></pre>

<p>我想将 ViewControllerC 中的数据传回给 A。这怎么可能,因为 A 没有对 C 的引用,我该如何实现委托(delegate)?</p>

<p>****编辑:这都是以编程方式完成的,所以我不能使用 unwind segues</p>

<p>***** 解决方案 *******</p>

<p>我发现的最佳解决方案是在 VC A 中添加一个观察者,并在关闭 VC 时将对象发布到 VC C:</p>

<pre><code>self.presentingViewController?.presentingViewController?.dismiss(animated: true) {
      NotificationCenter.default.post(name: Notification.Name(&#34;UpdateKeywords&#34;), object: self.account)
    }
</code></pre>

<p>不要忘记在 deinit() 中删除 VC A 中的观察者</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我通常会在 b 中创建对 A 的引用,然后在 C 中创建对该引用的引用,然后,在您传回 A 之前,对引用进行更改,然后调用 <code>self. presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)</code></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何从模态视图 Controller 将数据传递回 View 控件,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40363377/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40363377/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何从模态视图 Controller 将数据传递回 View 控件