Swift
UIViewController
custom initializer
iOS development
subclassing

How do I make a custom initializer for a UIViewController subclass in Swift?

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Creating a custom initializer for a UIViewController subclass in Swift can greatly enhance the modularity and reusability of your code. In app development, view controllers are the building blocks for defining user interface and user interaction. By customizing the initialization of these view controllers, you ensure that your views are configured conveniently and reliably at runtime.

Understanding UIViewController Initialization

When you create custom initializers for a UIViewController, you step away from the traditional process involving Storyboards or XIB files. Instead, you instantiate the view controller programmatically. To implement a custom initializer correctly, you must understand the lifecycle of a UIViewController and safely configure properties during initialization.

Basic Requirements

When you create a custom initializer in a UIViewController subclass, there are key rules to follow:

  1. Invoke a Designated Initializer: Swift dictates that subclasses must call a designated initializer of their superclass. For UIViewController, this is typically init(nibName:bundle:).
  2. Handle Coder Initializers: If you target Storyboards or XIB usage, you must also override the required init?(coder:) initializer even if it does nothing.
  3. Property Initialization: All properties must be initialized before calling a superclass initializer.
  4. Setting Up the View: Avoid manipulating views during initialization since the UI is not fully loaded. Use lifecycle methods like viewDidLoad() for view configurations.

Implementing a Custom Initializer

Below is an example of a custom initializer for a UIViewController subclass in Swift. We will create a simple view controller with a customizable title and background color.

  • Custom Properties: titleString and bgColor are configured during initialization, allowing the view to be personalized based on different states.
  • Custom Initializer: The initializer takes parameters title and backgroundColor, assigns them to the properties, and invokes the superclass's initializer.
  • viewDidLoad(): Properties like backgroundColor and title are applied in the viewDidLoad method to ensure the view is initialized correctly.
  • Missing Required Initializer:
    • If your subclass is intended to work with storyboards or XIBs, not implementing init?(coder:) will cause runtime errors.
  • UI Configuration Errors:
    • Accessing views directly inside the initializer can result in incomplete UI setup as views are fully accessible only after viewDidLoad().
  • Dependency Injection: Custom initializers have an advantage in facilitating dependency injection, making your code more testable and maintainable.
  • Unit Testing: By providing initializer-based configurations, UI testing and functional testing are simplified.
  • Compatibility: Ensure that your design remains flexible, accommodating both code-based and IB-based view creations through appropriate initializers.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms