编码自定义 SplitViewController - 我什么时候应该调用 viewWillAppear、viewDidAppear 等......?

发布于 2024-10-04 01:14:01 字数 818 浏览 6 评论 0原文

我正在从头开始编写自己的 SplitViewController (即通过子类化 UIViewController 而不是 UISplitViewController )。

它有两个子视图控制器(一个用于左侧面板,一个用于详细右侧面板),我需要向其发送适当的消息(viewWillAppear、viewDidAppear、viewWillDisapppear 和 viewDidDisappear)。

当我的自定义 SplitViewController 收到这些消息时,我已经转发这些消息并且工作正常。然而,当两个子视图控制器中的任何一个被新的子视图控制器替换时,我正在努力弄清楚何时发送它们,新的子视图控制器也需要接收这些消息。我正在正确添加新 UIViewController 的视图,但未充分调用消息。

我最初的方法是在子 viewController 的 setter 中调用它们,调用 viewWillDisappear 到即将发布的 UIViewController ,并调用 viewWillAppear 到新的UIViewController 设置,但这个是在 viewDidLoad 之前执行的,因此我认为是错误的。

我还看到 UIView 有一个方法 didAddSubview: ,它可能有助于了解何时在相应的 UIViewController 上调用 viewDidAppear

任何帮助将不胜感激!

I'm writing my own SplitViewController from scratch (i.e. by subclassing UIViewController and not UISplitViewController).

It has two sub-viewControllers (one for the left panel and one for the detail right panel), to which I need to send the appropriate messages (viewWillAppear, viewDidAppear, viewWillDisapppear and viewDidDisappear).

I am already forwarding those messages when my custom SplitViewController receives them and it works fine. However I am struggling to figure out when to send them when any of the two sub-viewcontrollers is replaced by a new one, which also needs to receive those messages. I am adding the view of the new UIViewController properly but the messages are not called adequately.

My initial approach was to call them in the setter of the sub-viewControllers, calling viewWillDisappear to UIViewController about to be released and viewWillAppear to the new UIViewController set, but this one is executed before viewDidLoad and therefore I presume is wrong.

I have also seen that UIView has a method didAddSubview: that might be useful to know when to call viewDidAppear on the correspondent UIViewController.

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2024-10-11 01:14:01

如果你想镜像 UISplitViewController,似乎最好只使用虚拟的 UIViewController,在调用每个方法时打印出来。

至于您当前的 viewWillDisappear、viewWillAppear 和 viewDidLoad 的排序问题,只需执行以下操作:

-(void)setSomeViewController(UIViewController newVC)
{
    [oldVC viewWillDisappear];
    [newVC view]; // Causes newVC to load the view, 
                  // and will automatically call -viewDidLoad
    [newVC viewWillAppear];

    [oldVC.view removeFromSuperview];
    [self.view addSubview:newVC.view];

    //retain and release as appropriate
    // other stuff you'll need to mirror, etc. etc.
}

If you want to mirror UISplitViewController, it seems best to just have dummy UIViewControllers that print out whenever each method is called.

As for your current problem of the ordering of viewWillDisappear, viewWillAppear and viewDidLoad, just do:

-(void)setSomeViewController(UIViewController newVC)
{
    [oldVC viewWillDisappear];
    [newVC view]; // Causes newVC to load the view, 
                  // and will automatically call -viewDidLoad
    [newVC viewWillAppear];

    [oldVC.view removeFromSuperview];
    [self.view addSubview:newVC.view];

    //retain and release as appropriate
    // other stuff you'll need to mirror, etc. etc.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文