将.xib文件放在框架项目中的哪里? [英] Where to put .xib file inside framework project?

查看:113
本文介绍了将.xib文件放在框架项目中的哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我失去了半天的时间来解决这个问题,我无法在线看到直接的解决方案。

I lost half a day figuring this and I can't see a straight forward solution online.

我创建了一个iOS CocoaTouch框架。我有一些私人和公共课程,一切正常。
问题 是指 在该框架内添加.xib文件

I created an iOS CocoaTouch Framework. I have some private and public classes in it and it all works fine. The problem is when I add .xib file inside that framework.

在我的框架中,我想实例化.xib文件,我得到的错误是:

Inside my framework I want to instantiate .xib file and the error I am getting is:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/dino/Library/Developer/CoreSimulator/Devices/FEDECDC2-D4EA-441E-B53B-329A5B3DFB7A/data/Containers/Bundle/Application/78CEC01F-4C36-4143-A7D6-DDE6CCAC801B/MyApp.app> (loaded)' with name 'NUInAppMessageView''

我听说.xib文件不能包含在静态库中,但我认为它们应该可以在框架内使用。

I've heard that .xib files can not be contained inside static library but I think they should be able to be used inside the framework.

我的框架目标包含之前的Copy Bundle Resources,这个.xib文件是我在框架项目中创建时自动添加。

My framework target contains "Copy Bundle Resources" from before and this .xib file was automatically added when I created it inside framework project.

我已经在一些地方读过,应该在一个单独的。 bundle 目标中添加资源,但在创建新目标时没有bundle选项,我相信这是旧的静态库-days。

I've read on a few places that resources should be added in a separate .bundle target but there is no "bundle" option when creating new targets and I believe this is form the old static-library-days.

这是我用来初始化我的视图的代码(在框架内):

This is the code I am using to initialize my view (inside the framework):

NSBundle * frameworkBundle = [NSBundle bundleForClass:[self class]]; // fine (not nil)

NUInAppMessageView *view = [[frameworkBundle loadNibNamed:@"NUInAppMessageView" owner:self options:nil] firstObject]; // <-- CRASH

我试图将其分解为更多方法调用并计算出Nib本身正在创建:

I tried to break this into a more method calls and figured that Nib itself is getting created:

UINib *nib = [UINib nibWithNibName:@"NUInAppMessageView"
                                bundle:frameworkBundle]; // fine (not nil)
NSArray *objects = [nib instantiateWithOwner:self options:nil]; // <-- CRASH

我再也没有想法了。我尝试使用项目清理,删除派生数据,但他们似乎没有做到这一点。

I don't have ideas anymore. I tried with project Clean, deleting derived data but they don't seem to do the job.

我在这里遗漏了什么吗?

Am I missing something here?

推荐答案

在iOS项目的框架中放置Nib文件的位置?



我的答案背景:

我试图导出一些 UITableView 相关的实现,它有 UITableViewCell .xib 包含它的文件。最后他们会变成 .nib 文件! ;-)我指的是那些Nib文件如下:

Where to Put Nib Files within a Framework in iOS Project?

Background to my answer:
I was trying to export some UITableView related implementation and it had UITableViewCell .xib files packed with it. And they turn into .nib files at the end! ;-) And I was referring to those Nib files as follows:

[[NSBundle mainBundle] loadNibNamed:customCellID owner:nil options:nil];

说明:

但是这里的东西我做错了,我构建了一个静态Cocoa Touch Framework并尝试在另一个Application中使用它。那么在运行时什么会成为 MainBundle ?绝对是我的UITableView实现试图找到应用程序主包中的customCellID的Nib文件。

Explanation:
But here the thing I have done wrong is, I build a static Cocoa Touch Framework and try to use it within another Application. So at the run time what would become the MainBundle? Definitely my UITableView implementation trying to find out that Nib file for the customCellID in within the App's main bundle.

答案:

所以我修改了我的 UITableView 实现的上面代码片段,如下所示:

Answer:
So I altered my UITableView implementation's above code snippet as follows:

    NSString *frameworkBundleId = @"com.randika.MyFrameworkBundleIDHere";
    NSBundle *resourcesBundle = [NSBundle bundleWithIdentifier:frameworkBundleId];

    customCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:customCellID];
    if (customCell == nil) {
       NSArray *nibs = [resourcesBundle loadNibNamed:emptyCellID owner:nil options:nil];
       customCell = [nibs objectAtIndex:0];
    }

我用 UITableView构建的框架实现没有再次给出上述错误! : - )

And the framework I built out of my UITableView implementation didn't give the above error ever again! :-)

希望这个答案可能对那里的人有所帮助!

Hope this answer might be helpful to somebody out there!

干杯! : - )

这篇关于将.xib文件放在框架项目中的哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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