Where to place the .xib file inside the framework project?

I lost half a day realizing this, and I do not see a direct solution online.

I created the iOS CocoaTouch Framework. I have private and public classes and everything works fine. The problem is when I add the .xib file inside this frame .

Inside my frame, I want to instantiate an .xib file, and I get a message:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/dino/Library/Developer/CoreSimulator/Devices/FEDECDC2-D4EA-441E-B53B-329A5B3DFB7A/data/Containers/Bundle/Application/78CEC01F-4C36-4143-A7D6-DDE6CCAC801B/MyApp.app> (loaded)' with name 'NUInAppMessageView'' 

I heard that .xib files cannot be contained in a static library, but I think they should be accessible inside the frame.

My framework target contains β€œCopy Bundle Resources” and this .xib file was automatically added when I created it inside the framework project.

enter image description here

I read a few places where resources should be added to a separate .bundle target, but when creating new goals there is no β€œbundle” option, and I think this is a form of old static library days.

This is the code that I use to initialize my view (inside the frame):

 NSBundle * frameworkBundle = [NSBundle bundleForClass:[self class]]; // fine (not nil) NUInAppMessageView *view = [[frameworkBundle loadNibNamed:@"NUInAppMessageView" owner:self options:nil] firstObject]; // <-- CRASH 

I tried to break this down into more method calls and realized that the thread itself was being created:

 UINib *nib = [UINib nibWithNibName:@"NUInAppMessageView" bundle:frameworkBundle]; // fine (not nil) NSArray *objects = [nib instantiateWithOwner:self options:nil]; // <-- CRASH 

I have no more ideas. I tried with the Clean project by deleting the derived data, but they don't seem to do the job.

Did I miss something?

+6
source share
1 answer

Where to place Nib files within the framework in an iOS project?

Background of my answer:
I tried to export some related implementation of UITableView and it had UITableViewCell .xib files packaged with it. And they turn into .nib files at the end! ;-) And I meant these Nib files as follows:

 [[NSBundle mainBundle] loadNibNamed:customCellID owner:nil options:nil]; 

Explanation:
But here is what I did wrong, I am creating a static Cocoa Touch Framework and trying to use it in another application. So, at runtime, what will become the MainBundle ? Definitely my implementation of UITableView is trying to figure out this Nib file for customCellID inside the main application package.

Answer:
Therefore, I changed the implementation of the UITableView above the code snippet as follows:

  NSString *frameworkBundleId = @"com.randika.MyFrameworkBundleIDHere"; NSBundle *resourcesBundle = [NSBundle bundleWithIdentifier:frameworkBundleId]; customCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:customCellID]; if (customCell == nil) { NSArray *nibs = [resourcesBundle loadNibNamed:emptyCellID owner:nil options:nil]; customCell = [nibs objectAtIndex:0]; } 

And the framework that I built from my implementation of UITableView no longer produced the error above !:-)

Hope this answer might be useful to someone out there!

Hooray!: -)

+5
source

All Articles