我有一个自定义视图,其中包含以下代码:
@IBDesignable
class ABCMyView: UIView {
// (IBOutlets and other code omitted)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
private func commonInit() {
let nib = UINib(nibName: "ABCMyViewContents", bundle: NSBundle.mainBundle())
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
addSubview(view)
}
}
尝试在Interface Builder中查看ABCMyView时,会导致崩溃。尝试调试崩溃会发现,在尝试使用所有者实例化nib的行上崩溃。没有错误消息,只有一个最终在汇编代码(libobjc.A.dylib`objc_exception_throw:)中结束的堆栈跟踪。
以下是我在异常断点上尝试过的一些内容:
(lldb) po nib
<UINib: 0x78ef8f20>
(lldb) po nib.instantiateWithOwner(self, options: nil)
error: Execution was interrupted, reason: breakpoint 1.1.
The process has been returned to the state before expression evaluation.
当我运行应用程序时,一切都完美无缺,只有在尝试在Interface Builder中查看UIView时才会出现问题。
我如何从IBDesignable加载一个Nib文件?