Search code examples
iosobjective-cxibnsbundle

Failing to store xib files to an own bundle


In my project I've created an own Settings Bundle (called UIBundle), and dragged to it some of xib files. Then I'm trying to load them from bundle, for example this 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];
}

But every time I run my app, it crashes when trying to load a cell with the following message:

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'' 

Spent one day finding a solution without success. Any help would be appreciated.

P.S. Bundle is loaded successfully and any other resource besides xib files is accessible.


Solution

  • The only way to fix my issue was to follow the following steps:

    1. Create a XCode project with type: MacOS platform.
    2. From the section Framework & Library choose Bundle.
    3. Then add all the xib files to the project.
    4. Change Base SDK from Build Settings to Latest iOS.
    5. Build the project.
    6. Then expand Products folder and open the bundle file. There must be all the nib files.

    I copied them to my own bundle and everything worked.