当我尝试显示包中的 View 时,我就会崩溃。设置如下:
- 项目在应用的主包内包含一个包 CFramework.bundle
- CFramework.bundle 在其根目录中包含 GigyaFBPreviewController.xib 及其使用的图像
- GigyaFBPreviewController.m 位于项目引用的静态库中
代码:
NSString* bundlePath = [[NSBundle mainBundle] pathForResource:@"CBCFramework" ofType:@"bundle"];
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath];
GigyaFBPreviewController* gigya = [[GigyaFBPreviewController alloc] initWithNibName:@"GigyaFBPreviewController" bundle:bundle];
[self presentModalViewController:gigya animated:YES];
该代码在单击按钮后执行,并在最后一行崩溃。 GigyaFBPreviewController 只是一个 UIViewController,它使用默认的 initWithNibName:bundle:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Could not load NIB in bundle: 'NSBundle </Users/mjovanovic/Library/Application Support/iPhone Simulator/4.3.2/Applications/A40F8D71-EB88-4EB5-B9D3-CFD330C57F24/socialmediatest.app/CBCFramework.bundle> (not yet loaded)' with name 'GigyaFBPreviewController''
*** Call stack at first throw:
(
0 CoreFoundation 0x01ad85a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01c2c313 objc_exception_throw + 44
2 CoreFoundation 0x01a90ef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x01a90e6a +[NSException raise:format:] + 58
4 UIKit 0x00e050fa -[UINib instantiateWithOwner:options:] + 2024
5 UIKit 0x00e06ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
6 UIKit 0x00cbc628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
7 UIKit 0x00cba134 -[UIViewController loadView] + 120
8 UIKit 0x00cba00e -[UIViewController view] + 56
9 UIKit 0x00cbba3d -[UIViewController viewControllerForRotation] + 63
10 UIKit 0x00cb7988 -[UIViewController _visibleView] + 90
11 UIKit 0x00f5993c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
12 UIKit 0x00c3181e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
13 UIKit 0x00eb9619 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1381
14 UIKit 0x00cbe65d -[UIViewController presentModalViewController:withTransition:] + 3478
15 socialmediatest 0x000027bb -[socialmediatestViewController clicky:] + 283
etc
相关信息:
- 如果我从 CFramework.bundle 中取出 nib 文件并将其放入项目中,它就可以正常工作。但是,我需要将它打包成静态库来分发。
- 该 bundle 存在于 .app 内部,并且调用 [bundle pathForResource:@"GigyaFBPreviewController"ofType:@"xib"] 返回正确的路径。
- 如果我从 Nib 删除图像引用,则不会发生任何变化。所有图像均引用为 CFramework\image.png
- “(尚未加载)”错误消息确实很奇怪。我发现了很多帖子,人们在切换到 Xcode4 时遇到了相同的异常,但他们的解决方案对我不起作用。
* 解决方案*
xib 没有编译成 nib,无法加载,呵呵。感谢 Joshua Weinberg 在评论中提供以下链接!
最佳答案
首先,如果 bundle 没有正确创建,它将不会被加载。因此,为了创建正确的 bundle ,以下是创建 bundle 的步骤:
通过在 OS X -> Framework & Libraries 下选择名为 Bundle 的模板来添加新目标。
选择新创建的目标并将 BaseSDK 从 OSX 更改为最新的 iOS。
在构建短语 -> 复制 bundle 资源中添加您想要从 bundle 中使用的 .xibs、图像或其他资源。
在构建短语中添加 CoreFoundation 框架 -> 将二进制文件与库链接。
选择 iOS 设备编译目标。
将新创建的 bundle 从 Products 目录保存到某个位置。
现在将该包复制到您的主项目中。使用以下代码加载包:
NSString *path = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
您现在已设置好新 bundle 。
关于ios - 从 bundle 加载 NIB 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918047/