Benedict's Soapbox

Nib++ (or how to use the good bits of Storyboards)

Update 14th November 2014: I don’t recommend using the technique in this post. It’s based on a Luddite mindset and is a vain attempt at countering the inevitable evolution of UIKit.

Storyboards, introduced in iOS 5, have been a controversial addition to UIKit. I’m not a fan. However, there are some tasks which cannot be performed easily using plain nib files but can be performed with a storyboard:

Good news! It’s possible to get these benefits without having to use storyboards. How? Err, by using storyboards. In a nut shell the technique works by:

Creating the view controllers storyboard

  1. Create a new storyboard file and give it the same name as the view controller, e.g. EMKAmazingViewController.storyboard.
  2. The new storyboard will be empty. Drag a “View Controller” from the Object Library on to the canvas. (If your view controller is sub class of UITableViewController or UICollectionViewController then drag the appropriate object instead).
  3. First select the view controller by clicking on the yellow icon beneath the view, and then select the Identity Inspector. Change the Class from UIViewController to the view controller sub class.
  4. Finally select the Attributes Inspector and ensure that “Is Initial View Controller” is selected.

Step one is done! You’re now ready to configure the view controller:

Implementing initWithNibName:bundle:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([self class]) bundle:nibBundleOrNil];  
    return [storyboard instantiateInitialViewController];  

Note that we’re not returning self. This is perfectly acceptable. In fact, the reason why alloc & init are always performed together is so that instance can return an object other than self.

That’s it!