awakeFromNib 中的变量为零
Posted
技术标签:
【中文标题】awakeFromNib 中的变量为零【英文标题】:Variables is nil in awakeFromNib 【发布时间】:2016-11-09 11:54:46 【问题描述】:我创建了一个自定义 UIView,并使用对象 ConnectDetailItem 进行实例化。
我的自定义视图代码:
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
。为什么?
我的观点是这样的:let viewInfos = InfosConnectView(connectDetailsItem: self.connectDetailsItem, frame: CGRect(x: 0, y: 9, width: self.view.frame.width, height: 200))
我设置了断点并且在进入awakeFromNib函数之前connectDetailItem不是nil。
【问题讨论】:
查看查看地址。也许是不同的视图示例? 如果我注释掉 awakeFromNib() 中的所有行,视图显示正确。 您说问题不在于“正确”显示。无论如何,“connectDetailsItem”为零。请检查内存中的查看地址。您可以在调试器中执行此操作。它看起来像:“0x12345678” 尝试将您的所有代码从您的awakeFromNib
中放入一个函数中。并从init
和awakeFromNib
调用该函数。
Rikh 这个函数在哪里调用?
【参考方案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
【讨论】:
以上是关于awakeFromNib 中的变量为零的主要内容,如果未能解决你的问题,请参考以下文章