Mastering instantiateInitialViewController: A Guide to Efficient iOS Storyboard Navigation

Including one gotcha

Steven Curtis

--

Photo by Chris Scott on Unsplash

It took Apple until iOS13 to allow us to use instantiateInitialViewController(). Here is a guide about that, but also one gotcha you might fall into.

Docs: https://developer.apple.com/documentation/uikit/uistoryboard/1616213-instantiateinitialviewcontroller

Difficulty: Beginner | **Easy** | Normal | Challenging<br/>

This article has been developed using Xcode 15.0, and Swift 5.9

Terminology:

Storyboard: A way to graphically layout the UI in Xcode

UINavigationController: A container that stores view controllers in a stack

Introduction

Storyboards are a visual representation of the user interface. Personally I’d prefer to programatically instantiate view controllers yet work constrains often mean you need to work with storyboards.

Before iOS 13, developers had to navigate a more cumbersome path to inject properties in a view controller meaning that it’s difficult to understand the requirements of any particular view controller. Leveraging instantiateInitialViewController() means developers can understand the requirements for ay given view controller and make good decisions accordingly.

to achieve what instantiateInitialViewController() simplifies, marking its introduction as a significant enhancement to the developer experience. In this guide, we’ll dive deep into how to leverage instantiateInitialViewController() for efficient iOS Storyboard navigation, ensuring you’re equipped to make the most of this functionality.

Understanding instantiateInitialViewController

The problem to be solved

If you wish to inject properties using the storyboard you are likely to need to do something like the following:

final class SecondViewController: UIViewController {
var injectedProperty: String?

So injectedProperty is optional — but this doesn’t make much sense if we need to use that property in the construction…

--

--