我创建了一个自定义UIView,我使用对象ConnectDetailItem实例化。
我的自定义视图代码:
.as-console-wrapper{top:0;max-height:100%!important;}
在class InfosConnectView: UIView {
var view: UIView!
@IBOutlet weak var categorie: UILabel!
@IBOutlet weak var distance: UILabel!
@IBOutlet weak var followers: UILabel!
@IBOutlet weak var descriptionTextView: UITextView!
var connectDetailsItem:ConnectDetailsItem!
convenience init(connectDetailsItem:ConnectDetailsItem, frame:CGRect) {
self.init(frame: frame)
self.connectDetailsItem = connectDetailsItem
xibSetup()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func xibSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
view.layer.borderWidth = 1
view.layer.borderColor = UIColor(hex: "#DDDDDD").cgColor
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "viewInfosConnect", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
override func awakeFromNib() {
super.awakeFromNib()
//Infos Connect
self.categorie.text = "\(self.connectDetailsItem.category)"
self.categorie.text = "\(self.connectDetailsItem.category)"
if (self.connectDetailsItem.distance < 1000) {
self.distance.text = "\(self.connectDetailsItem.distance) m"
} else {
let distance:NSString = NSString(format: "%.01f", Float(self.connectDetailsItem.distance)/1000)
self.distance.text = "\(distance) km"
}
if(self.connectDetailsItem.followCount < 2) {
if(self.connectDetailsItem.followCount < 1) {
self.followers.text = "0 abonné"
} else {
self.followers.text = "\(self.connectDetailsItem.followCount) abonné"
}
} else {
self.followers.text = "\(self.connectDetailsItem.followCount) abonnés"
}
self.descriptionTextView.text = self.connectDetailsItem.description
}
}
中,awakeFromNib()
为connectDetailItem
。为什么?
我实现了我的观点:nil
我设置了断点,在进入awakeFromNib函数之前,connectDetailItem不是nil。
答案 0 :(得分:1)
我找到了解决方案。 我将代码放在函数loadViewFromNib中,如下所示:
func loadViewFromNib() -> InfosConnectView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "viewInfosConnect", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! InfosConnectView
//Infos Connect
view.categorie.text = "\(self.connectDetailsItem.category)"
if (self.connectDetailsItem.distance < 1000) {
view.distance.text = "\(self.connectDetailsItem.distance) m"
} else {
let distance:NSString = NSString(format: "%.01f", Float(self.connectDetailsItem.distance)/1000)
view.distance.text = "\(distance) km"
}
if(self.connectDetailsItem.followCount < 2) {
if(self.connectDetailsItem.followCount < 1) {
view.followers.text = "0 abonné"
} else {
view.followers.text = "\(self.connectDetailsItem.followCount) abonné"
}
} else {
view.followers.text = "\(self.connectDetailsItem.followCount) abonnés"
}
view.descriptionTextView.text = self.connectDetailsItem.description
return view
}