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.
In iOS development, Storyboards provide a visual canvas for designing and structuring your application's user interface (UI). However, there may be scenarios where you need to programmatically set the initial view controller of your application rather than relying solely on the storyboard setup. This flexibility can be crucial for applications that require dynamic UI setups, such as personalized user experiences or feature-based navigation.
Understanding Storyboards
Storyboards are XML files in Xcode that allow developers to design app interfaces visually. A storyboard includes scenes (each represented by a UIViewController) and the transitions between them called segues.
In a typical storyboard setup, you define a scene as the initial view controller by checking the “Is Initial View Controller” option in the Attributes Inspector. However, there are cases where setting this programmatically is beneficial.
Reasons to Set the Initial View Controller Programmatically
- Conditional Navigation: When the initial view depends on conditions, such as whether a user is logged in.
- Feature Flags: For applications with feature toggles where launching a specific view depends on which features are enabled.
- Testing and Development: During development, it can be useful to start at different view controllers quickly.
- Modular Design: In larger apps with multiple entry points.
Programmatic Setup in AppDelegate
The AppDelegate’s application(_:didFinishLaunchingWithOptions:) method is the typical location for setting the initial view controller programmatically. Here is a step-by-step approach:
- Remove the Initial View Controller from the Storyboard: Start by deselecting the "Is Initial View Controller" option in your storyboard.
- Access the Storyboard: Load your storyboard from the main bundle.
- Instantiate the Initial View Controller: Choose the appropriate view controller based on your logic.
- Set the Window's Root View Controller:
The above code assesses whether a user is logged in and sets the HomeViewController or LoginViewController accordingly. This logic is encapsulated within the application(_:didFinishLaunchingWithOptions:) method.
Example Code
Below is a complete example demonstrating setting the initial view controller programmatically in AppDelegate:
Key Considerations
- Performance: Instantiate only the view controllers required at start-up to prevent unnecessary memory usage.
- Initialization Logic: Place any global initial setup logic before setting the initial view controller.
- Testing: Write unit tests or UI tests to validate that the correct view controller is set under various conditions.
Alternatives to the AppDelegate Approach
- SceneDelegate (iOS 13+): With the introduction of
SceneDelegate, the method to set the initial view controller shifted fromAppDelegatein some instances. The principle remains the same but is applied within thescene(_:willConnectTo:options:)method.
Table of Key Points
| Topic | Explanation |
| Purpose | Dynamic UI setups based on conditions (e.g., user status). |
| Where to implement | AppDelegate or SceneDelegate methods, depending on iOS version. |
| Steps Required | Load storyboard, determine controller, set as root in window. Make visible. |
| Performance Consideration | Load only necessary controllers to optimize memory. |
| Alternatives | Use SceneDelegate for newer iOS versions supporting multiple scenes. |
By programmatically setting the initial view controller, developers gain flexibility in their app’s user interface flow, allowing for adaptive, user-centric designs and configurations. This method is advantageous in many scenarios, from handling user sessions to facilitating easier debugging during development.
Related reading
- Programmatically set the initial view controller using Storyboards
- 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?
.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.