将笔尖加载到视图时崩溃

时间:2019-01-17 05:25:09

标签: ios swift

我想创建一个笔尖并将其附加到我的viewcontroller。要创建笔尖,这就是我所做的...

首先,我添加了一个空白视图,并根据需要设计了该视图。然后我添加了一个快捷文件,并在其中添加了此代码...

class VerificationCodeView: UIView {
    @IBOutlet var contentView: UIView!
    @IBOutlet weak var mainLabel: UILabel!
    fileprivate weak var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) //CRASHES HERE
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }
}

然后在情节提要中的视图控制器中,添加了一个空白视图并将其设为VerificationCodeView类。

viewDidLoad的{​​{1}}中,这就是我所说的viewcontroller ...

VerificationCodeView

,并在@IBOutlet weak var verificationCodeView1: VerificationCodeView! //OUTLET fileprivate var loadingView: VerificationCodeView? 中,

viewDidLoad

但是,当断点到达类loadingView = VerificationCodeView(frame: verificationCodeView1.frame) 中的行Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil)时,它将崩溃。

我在这里做错什么了...?

编辑1:

这是显示的整个错误日志...

VerificationCodeView

3 个答案:

答案 0 :(得分:0)

请更新您的功能

private func commonInit() {
let uvNub: UIView = Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil) //CRASHES HERE
self.addSubview(uvNub)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]

}

答案 1 :(得分:0)

尝试以下代码,

fileprivate func commonInit(){

    let bundle = Bundle(for: type(of: self))
    bundle.loadNibNamed("VerificationCodeView", owner: self, options: nil)
    self.addSubview(containerView)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
    containerView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
    containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    containerView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}

答案 2 :(得分:0)

我发现您的代码有问题。

  1. 您有一个contentView的出入口。这意味着该视图已经添加到您的视图层次结构中。然后在您的commonInit()'addSubview(contentView)'中做错了。

  2. 未使用Bundle.main.loadNibNamed("VerificationCodeView", owner: self, options: nil)的结果。

您可以使用它从笔尖加载视图:

let verificationCodeView = UINib(nibName: "VerificationCodeView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VerificationCodeView

如果要获取VerificationCodeView的实例,请编写如下的类方法。

class func instantiateFromNib() -> VerificationCodeView {

    let verificationCodeView = UINib(nibName: String(describing:self), bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! VerificationCodeView
    return verificationCodeView
}

现在,只要您想要此视图,就可以像下面这样获得它:

let verificationCodeView = VerificationCodeView.instantiateFromNib()