UIViewController ChildViewController

November 1, 2011

I’m an iPad developer through and through. For me, the iPad was the first Apple product I bought with my own money, and the first Apple product for which I stood in line. What really bugged me when iOS 3.x came out on the iPad was just how little the API was catered for a tablet. With iOS 5 they really raise the bar, change the game, and (insert stupid platitude here).

###Custom Containers and Child View Controllers

Why does it matter? Well if you remember in iOS 4.x, any time you rotated the device, adjusted the layout, applied some Layer manipulations or adjusted the relative bounds, you NEVER got notified of the events inside the child view controllers.

With the iOS 5 release, iPad developers are really given the attention they deserve. With iOS 5, UIViewControllers are actually “containers”, sort of like UITabBarController and UINavigationConrtroller. This means we’ve got a lot more flexibility with the type of apps we can create and how we can present them on the screen. This is also very dangerous, because despite the higher resolution, the propensity to clutter our apps is much higher. So please, think of the children view controllers.


This is the first step and where it all starts. This allows any UIViewControllers you pass in to be contained with the view controller you call it from, but remember you still need to call addSubView. This is the general workflow:


It’s fairly straightforward, but here are a couple notes:

  • Do NOT remove the child viewcontroller’s View.
  • Do NOT call this from inside the child view controller, unless you want weird behavior.
  • This does NOT release the memory for your child viewcontroller automatically, by default though the MonoTouch GC may take care of this for you.

###Transition() / TransitionFromViewController()

This is an awesome new feature. One day I will re-write UINavigationController and this will make it all a piece of cake. It even comes with animations baked in!

Again, a couple notes:

  • You can only transition to views that have been explicitly added/contained using AddChildViewController UIVIewAnimationOptions have some new additions, check them out!
  • The “animations” and “completionHandler” parameters are both delegates. This means all your pre-animation targets and post-animation events are all accessible within one expression; cool stuff!
  • As with most things, make sure to call base.Transition(..) or [super transitionFromViewController:..]. If you override this, you’ll get weird behavior.

###Will / DidMoveToParentViewController

You’ll want to override these methods in your child view controllers to get notified of when they are added as children. You can’t do anything about it or prevent it per se, but at least they are nice enough to let you know.

I’d really like to see Xamarin provide C# ‘events’ so we can subscribe to these generically inside the container.

One more thing: One day, I hope to go to WWDC. Until then, here’s a really great video about view controller containment (sign in first): https://developer.apple.com/videos/wwdc/2011/?id=102

Hope you get a lot of use out of custom containment in iOS 5. Check out MonoTouch 5 with full support for iOS 5 and XCode 4 @ http://xamarin.com/monotouch


comments powered by Disqus