无法将 xib 文件存储到自己的包中

Failing to store xib files to an own bundle

在我的项目中,我创建了一个自己的设置包(称为 UIBundle),并将一些 xib 文件拖到其中。然后我试图从包中加载它们,例如这个 table view cell xib:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[self xibsBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    return cell;
}

- (NSBundle *) xibsBundle
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"UIBundle" ofType:@"bundle"];
    return [NSBundle bundleWithPath:path];
}

但是每次我 运行 我的应用程序在尝试加载带有以下消息的单元格时崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/containers/Bundle/Application/.../App.app/UIBundle.bundle> (not yet loaded)' with name 'MyCell'' 

花了一天时间寻找解决方案,但没有成功。任何帮助,将不胜感激。

P.S。 Bundle 加载成功,除 xib 文件外的任何其他资源都可以访问。

尝试使用 NSBundle class.

的 class 方法 bundleWithIdentifier:
- (NSBundle *) xibsBundle {

    return [NSBundle bundleWithIdentifier:@"UIBundle"];    
}

如果您知道包的路径,请尝试使用以下方法:

myBundle = [NSBundle bundleWithPath:@"Path\to\UIBundle"];

更多信息,请参考: https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html

解决我的问题的唯一方法是按照以下步骤操作:

  1. 创建类型为 MacOS 平台的 XCode 项目。
  2. Framework & Library 部分选择 Bundle
  3. 然后将所有的xib文件添加到项目中。
  4. 将 Base SDK 从构建设置更改为 Latest iOS
  5. 构建项目。
  6. 然后展开 Products 文件夹并打开捆绑文件。 必须有所有的 nib 文件。

我将它们复制到我自己的包中,一切正常。