gpt4 book ai didi

interface-builder - 未能呈现 ClassName : The agent threw an exception loading nib in bundle 的实例

转载 作者:行者123 更新时间:2023-12-03 13:45:18 25 4
gpt4 key购买 nike

当我在 Storyboard或另一个 nib 中包含自定义 IBDesignable View 时,代理崩溃并抛出异常,因为它无法加载 nib。

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'



这是我用来加载 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)
}
}

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

最佳答案

当 Interface Builder 呈现您的 IBDesignable View ,它使用辅助应用程序来加载所有内容。这样做的结果是 mainBundle在设计时与助手应用程序有关,而不是您的应用程序的mainBundle .您可以看到错误中提到的路径与您的应用程序无关:

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



加载 Nib 时,您依赖于传递 bundle: nil 的事实。默认为您应用的 mainBundle在运行时。
let nib = UINib(nibName: String(describing: StripyView.self), bundle: nil)

因此,您需要在此处传递正确的捆绑包。使用以下内容修复上述行:
let bundle = Bundle(for: StripyView.self)
let nib = UINib(nibName: String(describing: StripyView.self), bundle: bundle)

这将使 Interface Builder 从与自定义 View 类相同的包中加载您的 nib。

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

关于interface-builder - 未能呈现 ClassName : The agent threw an exception loading nib in bundle 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com