I am unable to add init method to the following UIViewController class. I need to write some code in the init method. Do i have to write init(coder) method? Even when I add
I was facing the same issue with Swift 5 in Xcode 12.0 and this is my solution in 2020.
class Foo: UIViewController {
private var bar: Bar!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setupController()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupController()
}
private func setupController() {
bar = Bar()
}
}
and an alternative I am using is to do the initialization inside loadView
function.
class Foo: UIViewController {
private var bar: Bar!
override func loadView() {
setupController()
}
private func setupController() {
bar = Bar()
}
}