Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active December 14, 2015 07:59
Show Gist options
  • Save bjhomer/5054702 to your computer and use it in GitHub Desktop.
Save bjhomer/5054702 to your computer and use it in GitHub Desktop.
Pattern for -init methods of controllers designed in a storyboard
- (id)init
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainInterface" bundle:nil];
self = [storyboard instantiateViewControllerWithIdentifier:@"MyControllerIdentifier"];
// Set up ivars here. 'self' won't be nil; if it is, you passed an invalid identifier
// and UIKit would have thrown an exception anyway.
return self;
}
@bwhiteley
Copy link

This results in an extra alloc.

How about:

+ (id)instance
{
  return [[UIStoryboard storyboardWithName:@"Foo" bundle:nil] instantiateViewControllerWithIdentifier:@"Bar"];
}

instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment