Coding custom SplitViewController - when should I call viewWillAppear, viewDidAppear, etc…?

こ雲淡風輕ζ 提交于 2019-12-11 07:06:15

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/4260762/coding-custom-splitviewcontroller-when-should-i-call-viewwillappear-viewdidap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!