44

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];

versus

#import "SomeViewController.h"

...

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

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

In either case, is someViewController effectively the same thing?

Benjamin Martin
  • 1,795
  • 3
  • 15
  • 17

7 Answers7

36

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.

dpassage
  • 5,423
  • 3
  • 25
  • 53
16

In the second case, the view controller will load its view from the storyboard and you will be happy.

In the first case, it won't. Unless you've taken other steps (like overriding loadView or viewDidLoad or creating a xib named SomeViewController.xib), you'll just get an empty white view and be sad.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • when I use second case I have great delay between instanttiation and actually controller load, I wonder why it's so bad? Here is log from iPhone 4S: `2015-03-03 20:42:39.692 App[5652:986462] Instantiate 2015-03-03 20:42:43.506 App[5652:986462] ViewDidLoad` almost 4 seconds. Don't try the other way yet, but maybe you encounter this? – Dima Deplov Mar 03 '15 at 18:01
  • The view controller loads its view on demand, because something asked for its `view` property. This usually doesn't happen until the system actually wants to put the view on the screen. What's happening between when you instantiate the VC and when it loads its view? Are you doing some slow operation like a network request? – rob mayoff Mar 03 '15 at 18:15
  • nope, I don't do anything, just wait for the load. I also tried perform a segue to this VC, but same load time =/ Think there is something with VC. I'll try to figure this out and if you don't mind I'll write here again. – Dima Deplov Mar 03 '15 at 18:20
  • if you interested, this issue was caused by custom font setted to text field, but that font wasn't copied to the device and this caused such delay. rdar://20028250 – Dima Deplov Mar 03 '15 at 22:42
5

In Swift you can do the same with,

var someVC = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController

You will need to give the Identifier in the Storyboard to the SomeViewController and tick the checkmark to Use Storyboard ID

Joseph
  • 5,793
  • 4
  • 34
  • 41
3

It is not the same thing. In the storyboard you probably have some UI elements laid out. They might have constraints and properties setup through the storyboard. When you instantiate the viewcontroller via the storyboard, you are getting all the instructions for where those subviews are and what their properties are. If you just say [SomeViewController new] you are not getting all the instructions that the storyboard has for the view controller.

A nice test will be to add a UIViewController to a storyboard and drag a red view onto it. Instantiate it using both methods and see what the differences are.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
2
simple swift 3 extension   
 fileprivate enum Storyboard : String {
        case main = "Main"
    }

    fileprivate extension UIStoryboard {
        static func loadFromMain(_ identifier: String) -> UIViewController {
            return load(from: .main, identifier: identifier)
        }

        static func load(from storyboard: Storyboard, identifier: String) -> UIViewController {
            let uiStoryboard = UIStoryboard(name: storyboard.rawValue, bundle: nil)
            return uiStoryboard.instantiateViewController(withIdentifier: identifier)
        }
    }

    // MARK: App View Controllers

    extension UIStoryboard {
        class func loadHomeViewController() ->  HomeViewController {
            return loadFromMain("HomeViewController") as! HomeViewController
        }
    }
Shan Shafiq
  • 1,018
  • 10
  • 10
0

In case you don't want to instantiate a new VC using instantiateViewControllerWithIdentifier but accessing the instance created by the storyboard from the AppDelegate:

  1. create a property in AppDelegate.h so it will be accessible from classes using it @property (nonatomic, strong) myViewControllerClass*vC;
  2. in viewDidLoad inside myViewControllerClass.m I access the shared instance of AppDelegate and feed the property with self: [AppDelegate sharedInstance].vC = self;

I had to use this solution in a complex storyboard and still can't get over the fact that I cannot find an easy way to access all (or at least the ones I need) objects in storyboard simply by addressing their identifiers.

Lookaji
  • 1,023
  • 10
  • 21
0

another thing to check for is if the viewcontroller that's throwing the error has a storyboardIdentifier, you can check the storyboard xib file.

the identifier was missing in my case, the error stopped when i added it

megaKertz
  • 484
  • 6
  • 11