IBOutlets发生了一些奇怪的事情。 IBOutlet属性在从xib加载的自定义视图后无n
在代码中,我尝试访问此属性,但它们是nil
。代码:
class CustomKeyboard: UIView {
@IBOutlet var aButt: UIButton!
@IBOutlet var oButt: UIButton!
class func keyboard() -> UIView {
let nib = UINib(nibName: "CustomKeyboard", bundle: nil)
return nib.instantiateWithOwner(self, options: nil).first as UIView
}
override init() {
super.init()
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
// MARK: - Private
private func commonInit() {
println(aButt)
// aButt is nil
aButt = self.viewWithTag(1) as UIButton
println(aButt)
// aButt is not nil
}
}
你在哪里访问它们除了commonInit? – 2015-02-07 17:34:27
@AshrafTawfeeq现在无处可去。我不能正常初始化这个类。 干净的项目也没有帮助 – Sk0prion 2015-02-07 18:17:11
在初始化时,Outlet尚未设置,因为视图尚未加载。您的第二个日志工作,因为访问视图导致它被加载。 – rdelmar 2015-02-07 18:18:13