gpt4 book ai didi

ios - 如何向作为 UIViewController 子类的 parentViewController 发送消息?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:57 24 4
gpt4 key购买 nike

我有一个 UIViewController (MyViewController) 和另一个 View Controller ,我通过 MyViewController 以模态方式呈现(称之为 SecondViewController)。我希望能够使用

从 SecondViewController 向 MyViewController 发送消息
[self.parentViewController hideSecondViewController];

但是由于 parentViewController 被定义为一个 UIViewController,而 hideSecondViewController 不是 UIViewController 的一个方法,我收到一条警告说“UIViewController 可能不会响应‘hideSecondViewController’”。它工作正常,因为它可以在程序期间成功发送消息,但是由于我在 MyViewController 中#import SecondViewController,所以我无法在 SecondViewController 中#import MyViewController。有什么办法解决这个问题吗?

最佳答案

When it comes time to dismiss a modal view controller, the preferred approach is to let the parent view controller do the dismissing. In other words, the same view controller that presented the modal view controller should also take responsibility for dismissing it whenever possible. Although there are several techniques for notifying a parent view controller that it should dismiss its modally presented child, the preferred technique is delegation.

In a delegate-based model, the view controller being presented modally must define a protocol for its delegate to implement. The protocol defines methods that are called by the modal view controller in response to specific actions, such as taps in a Done button. The delegate is then responsible for implementing these methods and providing an appropriate response. In the case of a parent view controller acting as a delegate for its modal child, the response would include dismissing the child view controller when appropriate.

阅读更多信息 View Controller Programming Guide for iOS .


附言:

since I #import SecondViewController in MyViewController, I can't #import MyViewController in SecondViewController.

要解决循环依赖问题,您可以使用 forward declaration .

关于ios - 如何向作为 UIViewController 子类的 parentViewController 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com