在iOS项目中使用捆绑包时出错/如何创建正确的捆绑包?

时间:2011-11-02 09:00:40

标签: ios xcode static-libraries

我正在尝试在iOS项目中包含静态库。我导入了整个静态库项目,然后我在'目标依赖项'和'链接二进制文件'中链接了lib,如下所述:http://www.applausible.com/blog/?p=657

但是在测试应用程序时我遇到了这个错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/alexis/Library/Application Support/iPhone Simulator/5.0/Applications/9811107D-0D7E-4178-ACCE-BF43E3043770/PlazappPartnerTest.app> (loaded)' with name 'LauncherView''
*** First throw call stack:
(0x188a052 ......

我猜它无法访问.xib文件,但由于它包含在我的库中,我不知道为什么。 如何让主项目“看到”.xib文件? (如果这确实是问题......)

编辑:

我尝试制作一个包含.xib文件的包,并将其包含在我的主项目中,但我无法使用此包。我试过这个:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

NSError *error;
[bundle loadAndReturnError:&error];

NSLog(@"bundle error : %@", [error userInfo]);

我收到了这个错误:

NSLocalizedDescription = "The bundle \U201cBundleName.bundle\U201d couldn\U2019t be loaded because its executable couldn\U2019t be located.";
NSLocalizedFailureReason = "The bundle\U2019s executable couldn\U2019t be located.";
NSLocalizedRecoverySuggestion = "Try reinstalling the bundle.";

有什么问题?

我想我无法加载包含非可执行文件资源的包,例如.xib文件。我是对的吗?

但是,如何从捆绑中加载我的.xib?

1 个答案:

答案 0 :(得分:3)

在Cocoa编程中经常有多种方法可以获得捆绑。我在这方面取得了成功:

NSBundle *libBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"BundleName" withExtension:@"bundle"]];

然后用这个加载笔尖:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:libBundle];

您对loadAndReturnError:的此特定调用会加载该包的可执行代码。由于这个bundle可能没有任何可执行代码,只是库需要的资源集合,它会给你这个错误。

修改(回复评论):

我通过向我的库项目中添加一个新目标来创建我的包。从Mac OS X软件包模板开始,我将所有图像,.xib和其他资源添加到“复制软件包资源”构建阶段。我将基础SDK更改为iOS(最新)。然后我将捆绑包添加到库的依赖项和最终应用程序的“复制捆绑资源”构建阶段。

如果您只是尝试将.xib文件添加到文件夹方法,则.xibs不会编译为nib,并且iOS无法读取它们。