【问题标题】:IBDesignable crashes when attempting to instantiate a nib尝试实例化笔尖时 IBDesignable 崩溃
【发布时间】: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


    【解决方案1】:

    看起来这是正在使用的 NSBundle 的问题。我能够通过将NSBundle.mainBundle() 更改为NSBundle(forClass: self.dynamicType) 来解决问题。

    我从这篇博文中得到了这个想法:Create an IBDesignable UIView subclass with code from an XIB file in Xcode 6

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2019-03-29
      • 2015-09-24
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多