UIViewController containment

Today at WWDC I took a bunch of code to one of the Cocoa Touch frameworks labs to have one of the engineers look over a framework we’ve been produced which involves a massive amount of UIViewControllers.

Suffering from a healthy case of Imposter Syndrome, I was expecting a good smack with the wooden paddle, and a few minutes in time-out. Instead, everything looked AOK. However, he did pick up on one thing – I should’ve been using view controller containment.

Originally, i’ve just been doing the following when adding a child view controller:

[self.view addSubview:childVC.view];

That’s all well and good, however he instructed me to add the sub VC as a child before adding it’s view.

[self addChildViewController:childVC];
[self.view addSubview:childVC.view];

This ensures that a healthy link is retained between the child and parent VC, and messages (such as orientation changes etc) are automatically passed from the parent onto the child.

If I’d had my head buried deeper in the documentation, yes i would have learned this early on. If you’re like me and missed this little nugget of joy, I hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *