Instantiate View Controller from Storyboard VS. Creating New Instance

Instantiate View Controller from Storyboard vs. Creating New Instance

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.

In iOS, does a segue instantiate the new-to-be-used view controller? Or is it already instantiated?

Apple's documentation says,

When the storyboard runtime detects a custom segue, it creates a new instance of your class, configures it with the view controller objects, asks the view controller source to prepare for the segue, and then performs the segue.

( https://developer.apple.com/reference/uikit/uistoryboardsegue )

So the destination UIViewController is instantiated by the segue just before sending prepareForSegue to the source UIViewController.

So to answer your questions directly, it is "yes" to both questions:

In iOS, does a segue instantiate the new-to-be-used view controller?

Yes, the segue does instantiate the destination view controller.

Or is it already instantiated?

Yes, by the time your prepareForSegue is called, it is already instantiated - immediately beforehand.

UPDATE: As @Jeffery_Thomas commented, this is trivially easy to demonstrate by adding an NSLog() line to your destination view controller's init.

Instantiate and Present a viewController in Swift

This answer was last revised for Swift 5.4 and iOS 14.5 SDK.


It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)

Make sure to set myVCID inside the storyboard, under "Storyboard ID."

Sample Image

UIViewController - alloc and init vs. instantiate?

[[MyObject alloc] init] creates a new object. It's not retrieving an object from a Storyboard, just allocating memory for it and instantiating it.

instantiateViewControllerWithIdentifier: creates a new view controller (if the identifier exists in the storyboard) and configures it according to how the view controller was configured object in the Storyboard file.

Both cases will create a new instance for each call.


If you have configured a view controller in the Storyboard (for example connected outlets, actions, etc.) and you want to retrieve it, you should read it from the Storyboard. If you would create a new instance (not from the Storyboard) it would not have this configuration.

ViewController class creates new instance when creating view, but it should use an existing one

How to show the table view depends on how you want to show it, and what is on the screen before that. Normally, you would use a segue to transition from the current controller (the one that you button is in) to the table view. To do that you could use performSegueWithIdentifier:sender: where the identifier parameter is a name you give to the segue in IB. But you can do it with no code at all by control dragging from the button in your one view to the table view controller's view and choosing modal for the type of segue -- then, a button click will cause that table view to appear on screen.

You shouldn't alloc init it at all in your code if you're using a storyboard -- the segue instantiates the new view controller for you.



Related Topics



Leave a reply



Submit