Benjamin Martin Benjamin Martin - 2 years ago 166
Objective-C Question

Instantiate View Controller from Storyboard vs. Creating New Instance

What is the functional difference between instantiating a View Controller from the storyboard and creating a new instance of it? For example:

#import "SomeViewController.h"


SomeViewController *someViewController = [SomeViewController new];


#import "SomeViewController.h"


UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:@"SomeViewController"];

In either case, is
effectively the same thing?

Answer Source

The main difference is in how the subviews of your UIViewController get instantiated.

In the second case, all the views you create in your storyboard will be automatically instantiated for you, and all the outlets and actions will be set up as you specified in the storyboard.

In the first, case, none of that happens; you just get the raw object. You'll need to allocate and instantiate all your subviews, lay them out using constraints or otherwise, and hook up all the outlets and actions yourself. Apple recommends doing this by overriding the loadView method of UIViewController.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download