Coding custom SplitViewController - when should I

2019-07-24 00:29发布

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条回答
贪生不怕死
2楼-- · 2019-07-24 01:16

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.
}
查看更多
登录 后发表回答