ios - 从 bundle 加载 NIB 时出现异常

标签 ios xcode4.2 bundle nib ios5

当我尝试显示包中的 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 的步骤:

  1. 通过在 OS X -> Framework & Libraries 下选择名为 Bundle 的模板来添加新目标。

  2. 选择新创建的目标并将 BaseSDK 从 OSX 更改为最新的 iOS。

  3. 在构建短语 -> 复制 bundle 资源中添加您想要从 bundle 中使用的 .xibs、图像或其他资源。

  4. 在构建短语中添加 CoreFoundation 框架 -> 将二进制文件与库链接。

  5. 选择 iOS 设备编译目标。

  6. 将新创建的 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/

相关文章:

ios - 在 Xcode 4.2 中移动源文件

xml - 如何在 iOS 的 MonoTouch/MonoGame 中从我的应用程序包中读取 XML 资源文件?

ios - 在当前 View Controller 下呈现新的 View Controller

ios - xCode 8 中裁剪的 iPad 模拟器

ios - 准备 Segue 函数无法正确传递数据

ios - 具有核心数据的主从应用程序

iphone - 如何在 Xcode 3.2 和 iOS 4.2 设备中运行和部署 Xcode 4.2 项目

macos - 使用 Mac Postgres App 很难 bundle pg

iphone - iOS应用程序更新,如何更新包中的plist?

ios - CFBundleDisplayName 在 xCode 中的位置