Creating View Controllers from Storyboard - codepath/ios_guides GitHub Wiki
Sometimes you have to instantiate view controllers that have been designed in Storyboard.
Step 1: Set a Storyboard ID
In the Storyboard, select the view controller that you want to instantiate in code. Make sure the yellow circle is highlighted, and click on the Identity Inspector. Set the custom class as well as the field called "Storyboard ID". You can use the class name as the Storyboard ID.
Step 2: Instantiate the view controller
In order to instantiate the view controller, you need a variable for the storyboard. Most storyboards are named "Main" (this is case sensitive!), but if your storyboard has a different filename, make sure you use that name.
// Create a reference to the appropriate storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Instantiate the desired view controller from the storyboard using the view controller's identifier
// Cast it as the custom view controller type you created in order to access its properties and methods
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController") as! CustomViewController
On iOS 13 and later, you can avoid the forced cast by using the generic instantiateViewController(identifier:creator:) method instead — it infers the view controller's type from your type annotation (the creator parameter defaults to nil, so you can omit it):
let customViewController: CustomViewController = storyboard.instantiateViewController(identifier: "CustomViewController")
Note: this snippet only compiles when your project's minimum deployment target ("iOS Deployment Target" in Build Settings, labeled "Minimum Deployments" on the target's General tab in newer Xcode versions) is iOS 13.0 or later. If your project still supports older iOS versions, keep using the instantiateViewController(withIdentifier:) form above, or choose between the two at runtime with if #available(iOS 13.0, *).