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.
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:
- Invoke a Designated Initializer: Swift dictates that subclasses must call a designated initializer of their superclass. For
UIViewController, this is typicallyinit(nibName:bundle:). - Handle Coder Initializers: If you target Storyboards or XIB usage, you must also override the
required init?(coder:)initializer even if it does nothing. - Property Initialization: All properties must be initialized before calling a superclass initializer.
- 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:
titleStringandbgColorare configured during initialization, allowing the view to be personalized based on different states. - Custom Initializer: The initializer takes parameters
titleandbackgroundColor, assigns them to the properties, and invokes the superclass's initializer. viewDidLoad(): Properties likebackgroundColorandtitleare applied in theviewDidLoadmethod 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.
Related reading
- How do I make a dotted/dashed line in Android?
- How do I make a new line in swift
- How do I make an attributed string using Swift?
- How do I make an enum Decodable in Swift?
- How do I make UILabel display outlined text?
- How do I make WRAP_CONTENT work on a RecyclerView
- How do I modify the background color of a List in SwiftUI?
- How do I obtain crash-data from my Android application?
.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 courseNo 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 courseData 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.