来自外部 XIB 的 DidSet 崩溃
Crash on DidSet from external XIB
我有一个外部 XIB
,即 @IBDesignable
。
在显示此 XIB
的视图中,我有此插座。
@IBOutlet weak var digitizingButton: DigitizingButton! {
didSet {
digitizingButton.buttonIsBeingTouched = {[weak self] in
// bla bla
}
}
}
崩溃于
digitizingButton.buttonIsBeingTouched = {[weak self] in
EXC_BAD_ACCESS (code 2)
DigitizingButton
加载如下:
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
self.loadViewFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadViewFromNib()
}
/** Loads instance from nib with the same name. */
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: .init(String(describing: type(of: self))), bundle: bundle)
let myView = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
self.view = myView
self.addSubview(myView)
}
知道发生了什么事吗?提前致谢。
尝试改变你的初始化方法。
将 xib
文件的第一个视图中的 IBOutlet
添加到 swift 文件:
@IBOutlet var containerView: UIView!
然后按如下方式编辑您的 loadViewFromBib
方法:
func loadViewFromNib() {
Bundle.main.loadNibNamed("DigitizingButton", owner: self, options: nil)
addSubview(containerView)
containerView.frame = self.bounds
containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
// Other initialization here
}
我有一个外部 XIB
,即 @IBDesignable
。
在显示此 XIB
的视图中,我有此插座。
@IBOutlet weak var digitizingButton: DigitizingButton! {
didSet {
digitizingButton.buttonIsBeingTouched = {[weak self] in
// bla bla
}
}
}
崩溃于
digitizingButton.buttonIsBeingTouched = {[weak self] in
EXC_BAD_ACCESS (code 2)
DigitizingButton
加载如下:
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
self.loadViewFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.loadViewFromNib()
}
/** Loads instance from nib with the same name. */
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: .init(String(describing: type(of: self))), bundle: bundle)
let myView = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
self.view = myView
self.addSubview(myView)
}
知道发生了什么事吗?提前致谢。
尝试改变你的初始化方法。
将 xib
文件的第一个视图中的 IBOutlet
添加到 swift 文件:
@IBOutlet var containerView: UIView!
然后按如下方式编辑您的 loadViewFromBib
方法:
func loadViewFromNib() {
Bundle.main.loadNibNamed("DigitizingButton", owner: self, options: nil)
addSubview(containerView)
containerView.frame = self.bounds
containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
// Other initialization here
}