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

ios - PresentingViewController 如何获得通知其 PresentedViewController 自行解散?


                                            <p><p><strong>鉴于:</strong></p>

<ul>
<li>呈现 ViewController B 的 ViewController A</li>
<li>ViewController B <strong>没有对 ViewController A 的引用</strong>(除了隐含的 <code>presentingViewController</code> 属性)</li>
<li>ViewController B 自己调用 <code>dismiss</code> 并且什么也不做</li>
</ul>

<p><strong>我想要达到的目标:</strong></p>

<ul>
<li>ViewController A 想知道 ViewController B 何时被解雇以清理某些状态</li>
</ul>

<p><strong>限制:</strong></p>

<ul>
<li>我不想使用 KVO</li>
<li>我不想以任何方式修改 ViewController B 或其行为</li>
</ul>

<p><strong>到目前为止我发现了什么:</strong></p>

<ul>
<li><code>dismiss(animated:completion:)</code> <a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss" rel="noreferrer noopener nofollow">according to the documentation</a>将调用转发到它的 <code>presentingViewController</code>。但似乎 <code>dismiss(animated:completion:)</code> 是 <strong>not</strong> 调用的,而是一个私有(private)方法 <code>_performCoordinatedPresentOrDismiss:animated:</code>。</li>
<li>iOS <a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621430-presentingviewcontroller" rel="noreferrer noopener nofollow">documentation on <code>presentingViewController</code></a>具有误导性。它说“呈现的 ViewController 将此属性设置为呈现它的 ViewController ”,但事实并非如此。在 iOS 11 中,这将始终指向调用 <code>present</code> 的 VC 的根父 VC。同样的 <a href="https://developer.apple.com/documentation/uikit/uiviewcontroller/1621407-presentedviewcontroller" rel="noreferrer noopener nofollow">documentation on <code>presentedViewController</code></a>具有误导性。它说“调用该方法的 ViewController 将此属性设置为它呈现的 ViewController ”,这不是全部。调用 <code>present</code> 的 VC 层次结构中的每个 VC(其所有父 VC 和子 VC)都将指向相同的 <code>presentedViewController</code>。</li>
</ul></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在您的 ControllerA 中,将其命名为 <code>UINavigationControllerDelegate</code> 并用 <code>navigationController:didShowViewController</code> 标记 ControllerB 的表示(<code>isControllerBisPresented = true</code>)。当 B 的 <code>viewDidAppear</code> 时,检查 <code>isControllerBisPresented</code> 是否为真。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - PresentingViewController 如何获得通知其 PresentedViewController 自行解散?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47350720/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47350720/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - PresentingViewController 如何获得通知其 PresentedViewController 自行解散?