在Swift中初始化自定义视图的奇怪问题

时间:2018-04-29 13:55:25

标签: ios swift uiview

我已经使用.xib创建了一个自定义UIView,现在尝试通过init方法显示它,但我发现了奇怪的Thread 1: Fatal error: init(coder:) has not been implemented错误。 1.所以,我创建了UIView xib和swift文件:

override init(frame: CGRect) {
    super.init(frame: frame)

    createSubview()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func createSubview() {
    let newFormView = UINib(nibName: String(describing: NewProfileFormView.self), bundle: nil).instantiate(withOwner: self, options: nil).first as! NewProfileFormView
    self.addSubview(newFormView)
}

并将其从另一个ViewController调用为:

let newProfileViewController = UIViewController()
let newFormView = NewProfileFormView()

newProfileViewController.view = newFormView
self.navigationController?.pushViewController(newProfileViewController, animated: true)

但是上面提到的错误崩溃了。你能帮我找到我的错误吗?

0 个答案:

没有答案