【发布时间】:2015-10-19 18:46:34
【问题描述】:
我的自定义视图中有如下代码:
@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 文件?
【问题讨论】:
标签: xcode cocoa-touch interface-builder nib