Programmatically set the initial view controller using Storyboards
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
iOS development has been significantly eased by the introduction of Storyboards, which allow developers to design user interfaces visually. One fundamental aspect when working with Storyboards is setting the initial view controller—the entry point of your app's UI. This article will delve into programmatically setting the initial view controller using Storyboards, providing technical insights and practical examples.
Understanding Storyboards
Storyboards are XML-based layout files that let you design your app’s interface visually. Each scene in a storyboard represents a view controller and its associated views.
Initial View Controller
In a storyboard, the initial view controller is marked with an arrow, indicating that it's the start of your app's flow. However, there are scenarios where you might need to set this programmatically, giving you more dynamic control over your app's navigation logic.
Why Set the Initial View Controller Programmatically?
Dynamic Flows
Different user roles or states might require different initial screens. For instance, a logged-in user should directly access their dashboard, while a new user should land on the signup screen.
A/B Testing
You might want to test different initial flows to find which results in better user engagement.
Technical Implementation
Step-by-step Demonstration
Below are steps to set the initial view controller programmatically:
- Remove the Initial View Controller in Storyboard:
In Interface Builder, uncheck the "Is Initial View Controller" option for any view controllers. This ensures that storyboards don't automatically set an initial view controller. - Access the AppDelegate:
The AppDelegate is a central controller, managing global app states, which makes it a suitable place to set the initial view controller. - Modify the `AppDelegate.swift`:
- `window = UIWindow(frame: UIScreen.main.bounds)`: This initializes the window, which covers the device's screen.
- `UIStoryboard(name: "Main", bundle: nil)`: Instantiates the storyboard; "Main" is the default name, which could vary based on your project setup.
- `instantiateViewController(withIdentifier:)`: Fetches a storyboard's view controller based on the identifier set in the Interface Builder.
- `window?.rootViewController = initialViewController`: Sets the selected view controller as the root.
- User Defaults: Choose different controllers based on stored values.
- Authentication State: Redirect based on login status.
- Different user segments might see different onboarding flows.
- Direct users to region-specific content on app startup.
- When introducing breaking changes, guide users through an upgrade flow.
Related reading
- Programmatically switching between tabs within Swift
- Programming with SurfaceView and thread strategy for game development
- ProgressDialog is deprecated.What is the alternate one to use?
- Proper practice for subclassing UIView?
- Proper Realm usage patterns/best practices?
- Proper usage of the Alamofire's URLRequestConvertible
- Proper use cases for Android UserManager.isUserAGoat?
- Proper use of beginBackgroundTaskWithExpirationHandler
.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.