Ios 带有闭包初始化的UIViewController
我尝试创建Ios 带有闭包初始化的UIViewController,ios,swift,iphone,uiviewcontroller,Ios,Swift,Iphone,Uiviewcontroller,我尝试创建UIViewController: class CategoriesVC: UIViewController { let tableView = UITableView() var completionHandler: (Category)->Void? init(completionHandler: @escaping (Category)->Void) { super.init() se
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")
}
}