Notes, tips and tricks on iOS development.

UIViewController Containment - Part I

In an effort to support more complex interfaces, Apple introduced in iOS 5, the ability for developers to manage view controller containment. This allows apps to steer away from the use of view controllers such as UITabViewController, UINavigationController, etc.

Still, the idea of managing several child view controllers seems daunting to developers and for a good reason. Apple hasn’t made a good job of streamlining the use of containment.

Adding a view controller as a child of another view controller involves more than one step and failing to ackowdelge any of these could mean a potential bug. Thus, it is important to handle containment in a responsible way!

UIViewController objects have a property named childViewControllers, which is an array of view controllers that is mantained automatically.

Lets assume we have two view controllers named dadVC and kidVC. Of course, we’d like to add kidVC as a child of dadVC and present it in dadVC’s interface.

The first step would be for dadVC to send a addChildViewController: message to himself, passing kidVC as an argument. kidVC will be retained (since it’s added to dadVC’s childViewControllers array). It’s important to understand that this action will trigger dadVC’s viewDidLoad method.

It’s also worth mentioning that at this point, kidVC’s willlMoveToParentViewController: method is called automatically, however you will have to manually call didMoveToParentViewController: later on.

As a second step, we must add kidVC.view as a subview of dadVC.view or any of its subviews. It is at this point that kidVC’s methods viewWillAppear: and viewDidAppear: get called in that order.

In the last additional step involved. You will have to call the didMoveToParentViewContoller: method of kidVC manually, passing dadVC as the argument.

Don’t forget to set kidVC.view.frame for bonus points.

UIViewController *kidVC = ...;
[self addChildViewController:kidVC];
[self.view addSubview:kidVC.view];
[kidVC didMoveToParentViewController:self];
kidVC.view.frame = ...;

Removing a child view controller form a parent view controller follows a similar pattern:

[kidVC willMoveToParentViewController:nil];
[kidVC.view removeFromSuperview];
[kidVC removeFromParentViewController]

The rest will be handled automatically for you.

As you can see, it’s not too hard but not too straightforward, either.

Building Blocks

The blog you are reading was built using the Octopress blogging framework. It runs on Jekyll, which is a static site generator.

Unlike platforms like Wordpress or Blogger which offer a CMS approach to blogging, Octopress focuses on simple writing and fast deployment. This means that once you have configured your environment, maintaining and deploying your blog is a very pleasant experience.

The setup process, however, is the most daunting aspect of Octopress. There’s a reason why the platform promotes itself as ‘A blogging framework for hackers’. To get you up and running you will need to be familiar with:

  • Using of the command-line (Terminal)
  • Obtaining SSHs
  • Basic understanding of Ruby
  • Using GIT

However, it’s not so bad as it seems. Octopress’ documentation will guide you through the basic installation and setup and its deployment to GitHub.

All in all, a pretty straight-forward task.

First Steps


Welcome to NSRambling. This blog is intended to serve as a repository of notes, tips and tricks on iOS Development and other technologies.

Many times I find myself revisiting old projects where I came up with a certain solution for a certain problem. Trying to find that particular piece of code is often a lengthy taks so I figured it would be a nice idea to keep those in a centralized place and what better place than a blog so that I may also share these thoughts with others who might be having the same issue!

I also wanted to try out Octopress for the first time. My previous blogging attempts were unfruitful, in part because I don’t like Blogger or Wordpress. Doing simple tasks like getting a post up or configuring the blog itself are always a pain, plus I really enjoy using Markdown.

I think my next post should be about my personal experience setting up Octopress and deploying it on GitHub!

Until then,