在“ contentLabel.text = content”行找不到零错误
打开/关闭commonInit
class SuccessfulView: UIView{
@IBOutlet weak var contentLabel: UILabel!
convenience init(content:String ,frame: CGRect){
self.init(frame:frame)
contentLabel.text = content
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame) // calls designated initializer
}
func commonInit(){
Bundle.main.loadNibNamed("SuccessfulFooterContentView", owner: self, options: nil)
addSubview(contentLabel)
}
}
创建具有xib的UIView的子类
答案 0 :(得分:0)
问题是,如果您调用init(frame:)
中的UIView
,则不会由界面构建器(storyboard / Xib)初始化。
因此,您应该通过Xib初始化视图。就像-
var content: String = ""
static public func instance(content: String, frame: CGRect) -> SuccessfulView? {
let view = Bundle.main.loadNibNamed("SuccessfulFooterContentView", owner: self, options: nil) as? SuccessfulView
view?.frame = frame
// and save your content
view?.content = content
return view
}
然后在awakeFromNib函数中设置您的内容
override func awakeFromNib() {
super.awakeFromNib()
contentLabel.text = content
}
答案 1 :(得分:0)
您在说
Bundle.main.loadNibNamed("SuccessfulFooterContentView", owner: self, options: nil)
相信这会导致此变量
@IBOutlet weak var contentLabel: UILabel!
要设置。但是,除非将笔尖中的文件所有者设置为SuccessView类并且连接了contentLabel
出口,否则不会如此。