0

This question was raised in another post and I couldn't them to expound on what they meant. I was under the notion that the storyboard is an attribute of UIViewController because Apple's reference doc says var storyboard in the area that reads "Interacting with Storyboards and Segues". Apple UIViewController reference doc One of the comments read "The storyboard isn't a property or an attribute of either UIViewController" and I hoped they would expound on this. Could anyone help because now I am confused on if I'm reading or utilizing the reference docs correctly.

The initial question is here: original question

Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61

2 Answers2

2

In the documentation, the definition states:

var storyboard: UIStoryboard?

The storyboard from which the view controller originated.

You're correct in thinking that var storyboard is a property of UIViewController. The reason for your confusion is that your mental model is assuming that because UIViewController has a reference to storyboard, it must own it. storyboard is simply a reference to the storyboard that loaded your UIViewController. It should be looked at as more of a bottom-up relationship rather than top-down.

In practice, as a UIViewController, a common use case is to ask your storyboard to instantiate other UIViewControllers, which we can then present:

let viewControllerToPresent = storyboard?.instantiateViewController(withIdentifier: "presentedController")
present(viewControllerToPresent, animated: true, completion: nil)
1

The UIViewController class definitely has a storyboard property.

Your other question is asking about the instantiation and initialisation of the initial view controller. In the context of that question, what I believe was meant is that the UIViewController class does not have a storyboard attribute that it somehow knows before an instance is instantiated that lets it instantiate itself from a storyboard.

Rather, the UIStoryboard sets the storyboard property on the UIViewController instance after it has created it. This is necessary for segues to work.

Note that the storyboard property is an optional, and it won't have a value if the instance wasn't created by a storyboard.

Paulw11
  • 108,386
  • 14
  • 159
  • 186