Why this happens
UIViewController follows Swift’s strict designated initializer rules. When you subclass it and add any custom initializer, you must:
- Call a designated initializer of the superclass —
super.init()with no arguments doesn’t exist; the designated initializer isinit(nibName:bundle:). - Provide
required init?(coder:)— Because UIViewController adopts NSCoding, the compiler enforces this initializer. If you don’t implement it, you get “Required initializer init(coder:)”. - Avoid infinite recursion — A
convenience init()that callsself.init()without parameters loops forever because it resolves back to itself.
The errors “Must call a designated initializer of super class” and “Required initializer init(coder:)” appear together when you override one initializer but omit the others.
How to fix it
1. Complete initializer set (recommended)
Implement all three initializers. The convenience initializer gives you a clean no-argument API; the overridden designated initializer runs your setup; the required coder initializer satisfies NSCoding.
import UIKit
final class ViewController: UIViewController {
private var tap: UITapGestureRecognizer?
// Convenience: public no-arg initializer
convenience init() {
self.init(nibName: nil, bundle: nil)
}
// Designated: override the superclass designated initializer
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
}
// Required: NSCoding compliance
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func handleTap(_ sender: UITapGestureRecognizer) {
// handle tap
}
}
Key points:
– convenience init() delegates to self.init(nibName:bundle:) — not self.init().
– override init(nibName:bundle:) passes parameters through to super.
– required init?(coder:) uses fatalError when you don’t support storyboards/xibs. If you do, decode properties instead.
– Use #selector(handleTap(_:)) instead of the string-based Selector("handleTap:").
2. If you need parameters in the convenience initializer
Make stored properties implicitly unwrapped optionals so the other initializers compile without initializing them.
final class DetailViewController: UIViewController {
var itemID: Int!
var tap: UITapGestureRecognizer?
convenience init(itemID: Int) {
self.init(nibName: nil, bundle: nil)
self.itemID = itemID
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func handleTap(_ sender: UITapGestureRecognizer) { }
}
This pattern is used by Apple frameworks. The ! tells the compiler the value will be set before use.
3. When you actually load from a storyboard or xib
Replace fatalError with real decoding:
required init?(coder: NSCoder) {
super.init(coder: coder)
tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
}
// If you have custom properties:
required init?(coder: NSCoder) {
itemID = coder.decodeInteger(forKey: "itemID")
super.init(coder: coder)
tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
}
How to prevent it
- Always provide the trio:
convenience init(),override init(nibName:bundle:),required init?(coder:). - Delegate the convenience initializer to
self.init(nibName:bundle:), never toself.init(). - Pass
nibNameandbundleparameters through tosuperunchanged. - Use
#selectorfor selector references; it catches typos at compile time. - Mark properties that are set only in convenience initializers as implicitly unwrapped optionals (
Type!).
FAQ
Can I skip the convenience init and only override init(nibName:bundle:)?
Yes, but callers must always use ViewController(nibName: nil, bundle: nil). The convenience initializer gives a cleaner ViewController() API.
Why does convenience init() { self.init() } cause an infinite loop?
self.init() inside a convenience initializer resolves to the same convenience initializer because no other no-argument initializer exists. It recurses until stack overflow.
Do I need init(coder:) if I never use storyboards?
The compiler requires it because UIViewController conforms to NSCoding. Use fatalError("init(coder:) has not been implemented") to make the requirement explicit and crash early if something unexpectedly tries to decode your view controller.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.