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