[英]UIViewController with closure init
我嘗試創建UIViewController
:
class CategoriesVC: UIViewController {
let tableView = UITableView()
var completionHandler: (Category)->Void?
init(completionHandler: @escaping (Category)->Void) {
super.init()
self.completionHandler = completionHandler
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我得到這個錯誤:
Must call a designated initializer of the superclass 'UIViewController'
在這條線上:
super.init()
該錯誤清楚地表明您必須為UIViewController
調用指定的init
,在本例中為super.init(nibName:,bundle:)
。
另外, completionHandler
語法是錯誤的,這里是修復:
class CategoriesVC: UIViewController {
let tableView = UITableView()
var completionHandler: ((Category)->Void)?
init(completionHandler: @escaping ((Category)->Void)) {
super.init(nibName: nil, bundle: nil)
self.completionHandler = completionHandler
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.