编码自定义SplitViewController-我何时应调用viewWillAppear,viewDidAppear等...? [英] Coding custom SplitViewController - when should I call viewWillAppear, viewDidAppear, etc...?

查看:108
本文介绍了编码自定义SplitViewController-我何时应调用viewWillAppear,viewDidAppear等...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它有两个sub-viewControllers(一个用于左侧面板,一个用于右侧细节面板),我需要向其发送适当的消息(viewWillAppear, viewDidAppear, viewWillDisapppear and viewDidDisappear).

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).

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

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.

我最初的方法是在sub-viewControllers的setter中调用它们,将viewWillDisappear调用到即将释放的UIViewController,将viewWillAppear调用到新的UIViewController集合,但是此操作在viewDidLoad,因此我认为这是错误的.

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.

我还看到UIView具有方法didAddSubview:,该方法可能有助于了解何时在对应的UIViewController上调用viewDidAppear.

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

任何帮助将不胜感激!

推荐答案

如果要镜像UISplitViewController,似乎最好让虚拟的UIViewControllers在每次调用每个方法时都打印出来.

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

关于您当前对viewWillDisappear,viewWillAppear和viewDidLoad的排序问题,只需执行以下操作即可:

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.
}

这篇关于编码自定义SplitViewController-我何时应调用viewWillAppear,viewDidAppear等...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆