无法呈现ClassName的实例:代理在bundle中引发异常加载nib [英] Failed to render instance of ClassName: The agent threw an exception loading nib in bundle

查看:156
本文介绍了无法呈现ClassName的实例:代理在bundle中引发异常加载nib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在故事板或其他笔尖中包含我的自定义IBDesignable视图时,代理会崩溃并抛出异常,因为它无法加载笔尖。

When I include my custom IBDesignable view in a storyboard or another nib, the agent crashes and throws an exception because it can't load the nib.


错误:IB Designables:无法更新自动布局状态:代理引发了NSInternalInconsistencyException异常:无法在bundle中加载NIB:'NSBundle(loaded)',名称为'StripyView'

error: IB Designables: Failed to update auto layout status: The agent raised a "NSInternalInconsistencyException" exception: Could not load NIB in bundle: 'NSBundle (loaded)' with name 'StripyView'

这是我用来加载笔尖的代码:

Here's the code I use to load the nib:

override init(frame: CGRect) {
    super.init(frame: frame)
    loadContentViewFromNib()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    loadContentViewFromNib()
}

func loadContentViewFromNib() {
    let nib = UINib(nibName: String(StripyView), bundle: nil)
    let views = nib.instantiateWithOwner(self, options: nil)
    if let view = views.last as? UIView {
        view.frame = bounds
        view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
        addSubview(view)
    }
}

当我在模拟器中运行时,视图正确地从nib加载,为什么它不会在Interface Builder中显示?

The view loads from the nib correctly when I run in the simulator, why won't it display in Interface Builder?

推荐答案

当Interface Builder呈现您的 IBDesignable 视图时,它使用帮助应用程序加载一切。结果是设计时 mainBundle 与助手应用有关,而不是你的应用 mainBundle 。您可以看到错误中提到的路径与您的应用程序无关:

When Interface Builder renders your IBDesignable views, it uses a helper app to load everything. The upshot of this is that the mainBundle at design time is related to the helper app, and it's not your app's mainBundle. You can see that the path mentioned in the error has nothing to do with your app:


/Applications/Xcode.app/Contents/Developer /Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays

加载笔尖时,你依赖于以下事实:在运行时传递 bundle:nil 默认为你应用的 mainBundle

When loading the nib, you're relying on the fact that passing bundle: nil defaults to your app's mainBundle at run time.

let nib = UINib(nibName: String(describing: StripyView.self), bundle: nil)

所以相反,你需要在这里传递正确的包。使用以下内容修复上述行:

So instead, you need to pass the correct bundle here. Fix the above line with the following:

let bundle = Bundle(for: StripyView.self)
let nib = UINib(nibName: String(describing: StripyView.self), bundle: bundle)

这将使界面Builder会从与自定义视图类相同的包中加载您的nib。

That will make Interface Builder load your nib from the same bundle as your custom view class.

这适用于您的自定义视图从包中加载的任何内容。例如,本地化的字符串,图像等。如果您在视图中使用这些,请确保使用相同的方法并明确传入自定义视图类的包。

This applies to anything that's loaded from a bundle by your custom view. For example, localised strings, images, etc. If you're using these in your view, make sure you use the same approach and explicitly pass in the bundle for the custom view class.

这篇关于无法呈现ClassName的实例:代理在bundle中引发异常加载nib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆