How can I load storyboard programmatically from class?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Loading a storyboard programmatically is just a matter of instantiating UIStoryboard and then asking it for either its initial view controller or a specific scene identifier. The important distinction is whether you want the storyboard's configured entry point or a known view controller class tied to a storyboard id. Once you understand that split, programmatic storyboard loading becomes predictable instead of magical.
Instantiate the Storyboard First
A storyboard is identified by its file name without the .storyboard extension. You create it with UIStoryboard(name:bundle:).
That line does not create a screen yet. It only gives you access to the scenes defined in that storyboard.
Loading the Initial View Controller
If the storyboard has an initial view controller configured in Interface Builder, you can load it directly:
This is useful when a storyboard represents a flow and already has the correct entry point configured.
If you are bootstrapping the app manually, you can assign that controller to the window:
In practice, most apps do this in SceneDelegate or equivalent app startup code.
Loading a Specific View Controller by Identifier
If you want a particular scene, set a Storyboard ID in Interface Builder and instantiate it explicitly.
This is the most common programmatic pattern because it is explicit and type-checked with the cast.
Wrapping Storyboard Loading in the Class Itself
A common convenience pattern is to put the factory method on the view controller class.
Then the caller writes:
This keeps storyboard details out of the rest of the codebase.
When Programmatic Loading Helps
Programmatic storyboard loading is useful when:
- startup flow depends on login state or feature flags
- you want one class method to own the storyboard id
- you need to swap root view controllers at runtime
- you are mixing storyboards with coordinator-style navigation
It gives you more control, but it also means configuration mistakes show up at runtime if storyboard names or identifiers do not match.
Common Pitfalls
- Using the filename with
.storyboardincluded instead of just the storyboard name. - Forgetting to set a Storyboard ID for scenes you want to load by identifier.
- Assuming
instantiateInitialViewController()will work when no initial controller is configured. - Force-casting without a helpful error message when configuration is wrong.
- Spreading raw storyboard identifiers throughout the app instead of centralizing them.
Summary
- Create a storyboard with
UIStoryboard(name:bundle:). - Use
instantiateInitialViewController()for the configured entry scene. - Use
instantiateViewController(withIdentifier:)for specific scenes. - A class-level
instantiate()helper keeps storyboard details localized. - Most runtime failures come from mismatched storyboard names or missing scene identifiers.
Related reading
- How can I log each request/response using Alamofire?
- How can I make a button have a rounded border in Swift?
- How can I make a clickable link in an NSAttributedString?
- How can I make a function complete before calling others in an IBAction?
- How can I make a function execute every second in swift?
- How can I make a UITextField move up when the keyboard is present - on starting to edit?
- How can I make a weak protocol reference in 'pure' Swift without objc
- How can I make my custom objects Parcelable?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.