从捆绑包加载 NIB 时出现异常
当我尝试显示捆绑包中的视图时,我就会崩溃。设置如下:
- 项目在应用程序的主包内包含一个包 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 文件并将其放入项目中,它可以正常工作。但是,我需要将它捆绑在一起以与静态库一起分发。
- 该包存在于 .app 内部,调用 [bundle pathForResource:@"GigyaFBPreviewController" ofType:@"xib"] 返回正确的路径。
- 如果我从笔尖删除图像引用,则不会发生任何变化。所有图像都被引用为 CFramework\image.png
- “(尚未加载)”错误消息确实很奇怪。我发现了很多帖子,人们在切换到 Xcode4 时遇到了同样的异常,但他们的解决方案对我不起作用。
* 解决方案*
xib 没有编译成 nib,因此无法加载,呵呵。感谢 Joshua Weinberg 在评论中提供以下链接!
I get a crash as soon as I try to show a view from a bundle. Here's the setup:
- Project contains a bundle CFramework.bundle inside the app's main bundle
- CFramework.bundle contains GigyaFBPreviewController.xib and images it uses, in its root
- GigyaFBPreviewController.m is in a static library referenced by the project
Code:
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];
The code is executed after a button click, and crashes on the last line. GigyaFBPreviewController is just a UIViewController, and it uses the default 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
Relevant info:
- If I take the nib file out of CFramework.bundle and put it into the project, it works fine. However, I need it in a bundle to distribute with a static library.
- The bundle exists inside the .app and calling [bundle pathForResource:@"GigyaFBPreviewController" ofType:@"xib"] returns the correct path.
- If I remove the image reference from the nib, nothing changes. All images are referenced as CFramework\image.png
- The "(not yet loaded)" error message is really weird. I found a bunch of posts with people running into the same exception when switching to Xcode4, but their solutions didn't work for me.
* SOLUTION *
The xib was not compiled into a nib and it could not be loaded, duh. Thank you Joshua Weinberg for providing the link below in the comments!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果包没有正确创建,它将不会被加载。因此,为了创建正确的捆绑包,以下是创建捆绑包的步骤:
通过在 OS X 下选择名为捆绑包的模板来添加新目标 ->框架与库。
选择新创建的目标并将BaseSDK从OSX更改为最新iOS。
在构建短语中添加您想要使用的 .xibs、图像或其他资源 ->复制捆绑资源。
在Build Phrases中添加CoreFoundation框架->将二进制文件与库链接。
编译目标选择iOS设备。
将新创建的捆绑包从产品目录保存到某个位置。
现在将该包复制到您的主项目中。使用以下代码加载捆绑包:
您现在已设置新捆绑包。
First of all if a bundle is not created properly it will not get loaded. So in-order to create a proper bundle below are the steps for creating a bundle:
Add a new target by choosing a templete named bundle under OS X -> Framework & Libraries.
Select newly created target and change BaseSDK from OSX to Latest iOS.
Add .xibs, images or other resources which you want to use it from bundle in Build Phrases -> Copy Bundle Resources.
Add CoreFoundation framework in Build Phrases -> Link binary with Libraries.
Compile the target choosing iOS Device.
Save the newly created bundle from Products directory to some place.
Now copy that bundle into your main project. Load a bundle using below code:
You are now set with the new bundle.
如果您没有正确设置目标依赖项并且 Xcode 并行化您的构建,您也可能会收到此错误 - 即它将同时构建多个目标。
我在项目 A 构建子项目 B 并包含项目 B 资源包时遇到错误。项目 B 构建了子项目 C,该子项目构建了自己的项目 C 资源。项目 C 资源捆绑在项目 B 资源内。在 Xcode 中 >构建阶段>目标依赖项 - 如果我将 Project-B-Resources 放在 Project-B 之上,则捆绑包将被错误地打包。我需要确保首先列出 Project-B,以确保它正确触发 Project-C-Resources 构建......
... 有点拗口并让您了解上面的内容,但这是值得探索的事情您遇到嵌套项目内缺少笔尖的问题
https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeBuild.html
You can also get this error if you don't have your target dependencies setup correctly and Xcode parallelises your build - i.e. it will build multiple targets simultaneously.
I had an error where Project-A built sub Project-B and included Project-B-Resources bundle. Project-B built sub Project-C, which built it's own Project-C-Resources. Project-C resources were bundled inside Project-B resources. In Xcode > Build Phases > Target Dependencies - if I put Project-B-Resources above Project-B, then the bundle would be packaged up incorrectly. I needed to ensure Project-B was listed first, to ensure it triggered the Project-C-Resources build correctly.....
... bit of a mouthful and to get your head around the above, but it's something to explore if you're having problems with a missing nib inside nested projects
https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeBuild.html