Using a convenience init for a UITableViewController in a freshly created Xcode project with storyboards crashes on iOS 15, but works fine on iOS 14. The error message:
'UIViewController is missing its initial trait collection populated during initialization. This is a serious bug, likely caused by accessing properties or methods on the view controller before calling a UIViewController initializer.
How to reproduce
- Open Xcode 13
- Create a new project with Storyboards
- Select iOS 14.0 for deployment info
- Create an empty UITableViewController
class CustomTableViewController : UITableViewController {
var data:String?
convenience init() {
self.init(data: "test")
}
// Adding @objc fixes the crash
init(data: String) {
self.data = data
super.init(style: .plain)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
- Add the following to the main ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
_ = CustomTableViewController()
}
}
- Run project on iOS 15.0 simulator
The code works fine on an iOS 14.3 simulator. Prefixing init(data: String)
with @objc
fixes the problem.
Am I doing something wrong? Is this a bug in UIKit on iOS 15?