加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – 尝试实例化笔尖时IBDesignable崩溃

发布时间:2020-12-14 17:33:52 所属栏目:百科 来源:网络整理
导读:我的自定义视图中包含以下代码: @IBDesignableclass ABCMyView: UIView { // (IBOutlets and other code omitted) required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } override init(frame: CGRect) { super.init(fram
我的自定义视图中包含以下代码:

@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文件?

解决方法

看起来它正在被使用的NSBundle出现问题.我能够通过将NSBundle.mainBundle()更改为NSBundle(forClass:self.dynamicType)来解决此问题.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读