Storyboard doesn't contain a view controller with identifier
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When developing iOS applications with Swift, using storyboards is a common approach for designing user interfaces visually. Storyboards allow developers to lay out the structure and flow of the application by connecting view controllers and designing the user interface of each view. However, while working with storyboards, developers often encounter the error message: Storyboard doesn't contain a view controller with identifier. In this article, we'll explore the causes of this error, how to resolve it, and best practices to avoid it.
Understanding Storyboards and Identifiers
What is a Storyboard?
A storyboard is an interface builder file that shows the visual representation of all views and navigation paths in an iOS application. It's a comprehensive tool used for designing UIs in a more intuitive manner. Storyboards use scenes, which are individual view controllers.
What are View Controller Identifiers?
Each view controller in a storyboard can be assigned a storyboard identifier. This identifier is a string that uniquely identifies a view controller in the storyboard file. It is used primarily when instantiating view controllers programmatically.
Common Scenarios Leading to the Error
1. Missing Identifier
The most straightforward cause of the error is the absence of an identifier for the view controller you are trying to instantiate. When developers attempt to instantiate a view controller without setting an identifier, Xcode raises this error.
2. Incorrect Identifier Used
Another common cause is using an incorrect or misspelled identifier. The identifier must exactly match the string provided in the storyboard's Identity Inspector.
3. Wrong Storyboard
Developers may mistakenly reference the wrong storyboard or file path leading to the view controller instantiation. This results in the error if the referenced storyboard does not contain the requested view controller.
Example
Consider a scenario where you have a storyboard named Main.storyboard with a view controller designed to display user profiles. The identifier of this view controller is incorrectly defined or not set. The following Swift code attempts to instantiate it:
- Open the Identity Inspector on the right panel (
Option + Command + 3). - Locate the Storyboard ID field.
- Enter the desired identifier (e.g.,
ProfileVC). - Standardize Naming: Use a consistent naming convention for identifiers across all view controllers.
- Centralize Identifiers: Maintain a centralized file or struct to store all storyboard identifiers. This minimizes spelling mistakes and facilitates easy updates.
- Use Constants in Code: Reference identifiers from the centralized location:
- Storyboard Segue Usage: Utilize storyboard segues wherever possible to manage transitions, potentially reducing the frequency of programmatic instantiations.
Related reading
- Storyboard Segue From View Controller to Itself
- Storyboard warning prototype table cells must have reuse identifiers
- Streaming video from Android camera to server
- String Resource new line /n not possible?
- String search in string array in objective c
- stringByAppendingPathComponent is unavailable
- stringByAppendingPathComponent is unavailable
- structure vs class in swift language
.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.