Super easy UIViewControllers and their nib/xib

I don’t like initialising my UIViewControllers and their associated nib explicitly using a string

FlipsideViewController *viewController = [[FlipsideViewController alloc]
 initWithNibName:@"FlipsideView" bundle:nil];

But would rather my UIViewControllers know how to load themselves. This reduces the possibilities of typos, allows for only 1 place in code where your nib is referenced by name, and just looks better.

For example in FlipsideViewController.m I would add:

- (id)initWithNibName:(NSString *)nibNameOrNil 
bundle:(NSBundle *)nibBundleOrNil {
  if ((self = [super initWithNibName:@"FlipsideView" bundle:nibBundleOrNil]))  {
    // Initialization code		
  }
  return self;
}

Now you can simply initialise the ViewController using

FlipsideViewController *viewController = [[FlipsideViewController alloc] 
init];

Comments are closed.