具有闭包初始化的 UIViewController

UIViewController with closure init

提问人:YosiFZ 提问时间:7/6/2020 更新时间:7/6/2020 访问量:174

问:

我尝试创建: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()
iOS Swift iPhone UIViewController

评论


答:

3赞 Frankenstein 7/6/2020 #1

该错误明确指出必须调用指定对象 ,在本例中为 。initUIViewControllersuper.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")
    }
}