1

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!

monchote
  • 3,440
  • 2
  • 20
  • 20

1 Answers1

2

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.
}
David Liu
  • 9,426
  • 5
  • 40
  • 63
  • I've also noticed that sometimes -viewWillAppear: (or was it -viewWillDisappear?) gets called automatically when you add/remove a VC's view to the hierarchy. Admittedly this was done higher up the view hierarchy in order to implement custom view transitions, but worth keeping in mind. Additionally, I think there's something called UIViewControllerWrapperView... – tc. Nov 23 '10 at 22:20
  • As far as I know, the base UIViewController doesn't do this, but hierarchical types such as UINavigationController, UITabBarController, etc. will do this. – David Liu Nov 23 '10 at 22:41
  • Thanks for that David Liu. I also found that overriding -(void)didAddSubview:(UIView *)subview in your custom UIView can be quite useful so the custom UISplitViewController can notify its sub-UIViewControllers that viewDidAppear. – monchote Dec 07 '10 at 16:09