xcode iOS 静态框架中的“无法在包中加载 NIB”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28620358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:41:19  来源:igfitidea点击:

'Could not load NIB in bundle' from iOS Static Framework

iosxcodestatic-librariesxibios-frameworks

提问by KDaker

I am creating a static iOS framework (default template in Xcode 6) that includes xib files.

我正在创建一个包含 xib 文件的静态 iOS 框架(Xcode 6 中的默认模板)。

However I am having trouble loading the nib files when adding my framework to another app, I'm getting the following error:

但是,将我的框架添加到另一个应用程序时,我无法加载 nib 文件,出现以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/mobile/Containers/Bundle/Application/89B7C8F1-698A-4E0B-AD8F-4DB414A6001B/Sample.app> (loaded)' with name 'MyNibName''

I have tried several solutions, some outlined here. I still could not resolve the issue.

我尝试了几种解决方案,其中一些概述了here。我仍然无法解决问题。

I see a lot of references to this popular projectwhich is now unsupported because xcode has the built in feature. Should I be using this instead?

我看到很多对这个流行项目的引用,现在不受支持,因为 xcode 具有内置功能。我应该改用这个吗?

In my framework target I have the xib files included in the 'Copy Bundle Resources' build phase and I see them inside the built framework directory, however, I noticed that the structure of the framework produced does not follow the structure outlined in apple's documentation. Even when I changed the framework structure (using versions and linking the Headerand Resourcesfolders to the current version) I still couldn't resolve the issue.

在我的框架目标中,我在“复制捆绑资源”构建阶段包含了 xib 文件,并且我在构建的框架目录中看到了它们,但是,我注意到生成的框架的结构不遵循苹果文档中概述的结构。即使我更改了框架结构(使用版本并将文件夹HeaderResources文件夹链接到当前版本),我仍然无法解决问题。

I tried loading my nib in the following ways:

我尝试通过以下方式加载我的笔尖:

1loading the bundle by framework name, the bundle is nilin this case

1通过框架名称加载bundle,nil在这种情况下bundle

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"myframework" ofType:@"framework"];
resourcesBundle = [NSBundle bundleWithPath:resourceBundlePath];
self = [super initWithNibName:@"MyNibName" bundle:resourcesBundle];


2Creating a resource bundle and adding it to the framework. Also is nil. Note that this solution works if I add the bundle to the project separately, but that defeats the purpose of the framework.

2创建资源包并将其添加到框架中。也是nil。请注意,如果我将捆绑包单独添加到项目中,则此解决方案有效,但这违背了框架的目的。

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"resources" ofType:@"bundle"];
resourcesBundle = [NSBundle bundleWithPath:resourceBundlePath];
self = [super initWithNibName:@"MyNibName" bundle:resourcesBundle];


3Loading bundle by class, still did not work, however it returns the same app bundle as in [NSBundle mainBundle]. The FrameworkClassis any class embedded in the framework.

3按类加载包,仍然无效,但它返回与[NSBundle mainBundle]. 该FrameworkClass是嵌入在框架的任何类。

NSBundle* bundle = [NSBundle bundleForClass:[FrameWorkClass class]];  


4Loading the main bundle with [NSBundle mainBundle]which doesnt and shouldnt work.

4加载[NSBundle mainBundle]不应该也不应该工作的主包。



I think what is happening is that the resources of the framework are not being included in the final app.

我认为正在发生的事情是框架的资源没有包含在最终的应用程序中。

What am I missing? has anyone been able to use the default static framework template in xcode to make a framework with nib files?

我错过了什么?有没有人能够使用 xcode 中的默认静态框架模板来制作带有 nib 文件的框架?

回答by Srikanth

This should load your bundle.

这应该加载你的包。

NSString* const frameworkBundleID  = @"com.framework.bundleId";
NSBundle* bundle = [NSBundle bundleWithIdentifier:frameworkBundleID];

回答by Lorenzo

this work for me:

这对我有用:

import loginFramework/ClassA.h

导入登录框架/ClassA.h

NSBundle* resourcesBundle = [NSBundle bundleForClass:[ClassA class]];
ClassA * class = [[ClassA alloc]initWithNibName:@"ClassA" bundle:resourcesBundle];
UINavigationController * navigation = [[UINavigationController alloc] initWithRootViewController:class];

[self.window setRootViewController:navigation];

in app delegate in this case, and in the header of Framework add #import "ClassA.h"

在这种情况下,在应用程序委托中,并在框架的标题中添加 #import "ClassA.h"

if you want, I can send you an example

如果你愿意,我可以给你发一个例子

回答by Maneesh M

SWIFT v5

斯威夫特 v5

I have created a variable in constant file

我在常量文件中创建了一个变量

var frameworkBundle:Bundle? {
    let bundleId = "com.framework.bundleId"
    return Bundle(identifier: bundleId)
}

Then whenever I need bundle value I directly pass it like

然后每当我需要捆绑价值时,我都会直接传递它

collectionView.register(UINib(nibName: "MyCVCell", bundle: frameworkBundle), forCellWithReuseIdentifier: "MyCVCell")