Swift Custom ViewController initializers
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Custom initializers are a good way to enforce that a view controller always receives the dependencies it needs. The correct initializer pattern depends on how the view controller is created: purely in code, from a nib, or from a storyboard.
Programmatic View Controllers
If the view controller is created entirely in code, a custom initializer is straightforward. Initialize your stored properties first, then call the designated superclass initializer.
This pattern is excellent for dependency injection because callers cannot create the controller without providing the required input.
Storyboards Need coder-Based Initialization
Storyboards instantiate view controllers through init(coder:), so a plain init(userID:) will not be called automatically. For storyboard-backed controllers, the modern approach is to add a custom coder initializer and use the storyboard creator closure.
Then instantiate it like this:
That gives you storyboard layout plus real dependency injection, instead of setting required data after initialization and hoping nothing touches it too early.
Keep Initialization Focused
Initializers should capture required state, not perform view work. Avoid touching outlets, view geometry, or presentation logic in an initializer, because the view hierarchy is not loaded yet.
Good things to do in an initializer:
- store dependencies
- choose configuration flags
- set simple non-view state
Better left for later lifecycle methods:
- outlet access
- network requests tied to visible UI
- layout work
- navigation side effects
That separation keeps the controller easier to test and reason about.
Use Factories When Construction Gets Busy
If the initializer starts collecting several services, IDs, and feature flags, move the assembly into a factory instead of spreading it across the app.
This pattern helps when multiple coordinators or routers need to create the same screen consistently.
Storyboard Segues Need Extra Care
If navigation happens through storyboard segues, you do not control initialization directly in the same way. In that case, the traditional fallback is property injection during prepare(for:sender:). It works, but it is weaker than initializer injection because the destination can briefly exist in an incomplete state.
If the dependency is truly required, consider switching that screen to:
- explicit programmatic creation
- a storyboard creator closure
- a factory that owns scene assembly
That usually produces stronger invariants.
Common Pitfalls
The biggest pitfall is defining a custom initializer on a storyboard-backed view controller and assuming Interface Builder will use it. It will not unless you instantiate with the creator closure that matches the coder-based initializer.
Another common mistake is reading outlets or touching view during initialization. The view may not be loaded yet, so that work belongs later in the lifecycle.
Teams also overuse optional properties for required dependencies. If a value is required, make it a let and force callers to provide it during construction.
Finally, using fatalError in required init?(coder:) is reasonable only when that construction path truly must never be used.
Summary
- Use custom initializers to enforce required dependencies.
- For code-created controllers, initialize stored properties and call
super.init. - For storyboard controllers, use a
coder-based initializer plus the creator closure. - Keep initializers focused on state, not on view work.
- Prefer initializer injection over optional property injection when the dependency is required.
Related reading
.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.