【问题标题】:how to load nib files from different folders?如何从不同的文件夹加载 nib 文件?
【发布时间】:2011-12-31 21:32:54
【问题描述】:

我正在尝试根据运行时的某些条件从子文件夹中加载 NiB 文件。问题是当我调用方法 pathForResource 时代码能够找到 NIB 文件,但是当我运行应用程序时,我总是收到以下错误

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:'NSBundle

这是我的代码:

// The designated initializer. Override to perform setup that is required before the view is loaded.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

if(somecondition == true)
{        
    NSString* nibPath = [[NSBundle mainBundle] pathForResource:nibNameOrNil ofType:@"xib" inDirectory:@"CAS"];
    NSString* customPath = [nibPath stringByDeletingLastPathComponent];

    NSBundle* localeBundle = [NSBundle bundleWithPath:customPath];

    if (localeBundle!= nil) { 
        nibBundleOrNil = localeBundle;
    }
}

}


if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {

}

return self;
}

【问题讨论】:

  • 它是 [NSBundle MainBundle];而且我认为您对 Xcode 中所谓的“文件夹”的结构感到困惑。当 Xcode 创建文件夹时,它只是编辑项目的 XML 以将这些文件包含在伪文件夹包中。它从不真正写入文件系统。
  • 如果没有“文件夹”,那么“inDirectory”字段是什么?
  • 这应该暗示你引用了你的包中的一个文件,比如 NSHomeDirectory 或 NSDocumentsDirectory。

标签: ios xib nib loadnibnamed


【解决方案1】:

您的 NIB / XIB 应该包含在您的 xCode 项目中。它们应该在同一个文件夹中。您只能加载导入到项目中的 NIB / XIB。

【讨论】:

  • 我已经使用文件系统中的文件组织了我的 xcode 项目,一些 XIb 位于子文件夹中,我还将它们添加到我的 xcode 项目中。
  • 在您的 xCode 项目中,即使 XIB 位于 group(xCode 中文件资源管理器中的类似文件夹的图标)中,它们也被视为同一级别的根。只需调用 XIB 名称即可加载文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
相关资源
最近更新 更多