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:
UINavigationItem
, UIBarButtonItem
and UITabBarItems
UITableViewController
with static cellsGood 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:
initWithNibName:bundle:
so that it returns an instance of the view controller loaded from the storyboard.UITableViewController
or UICollectionViewController
then drag the appropriate object instead).Step one is done! You’re now ready to configure the view controller:
UINavigationItem
, UIBarButtonItem
and UITabBarItems
by dragging them on to the view. You’ll need to set the Top Bar and Bottom Bar appropriately to see this items. (Top Bar and Bottom Bar are set in the Attributes Inspector).UITableViewController
with static cells by using a UITableViewController
subclass.IBOutlet
on the view controller to connect to the child view controllers. The best alternative is to create an IBOutlet
for the container view and to poke around self.childViewControllers in viewDidLoad.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!