2
votes

i'm trying to put NIB/XIB files in a bundle I call Configuration.bundle. When I try to load the xib from my bundle the app crashes because it can't find the xib file.

NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]
pathForResource:@"Configuration" ofType:@"bundle"]];
[bundle load];
NSLog(@"bundle: %@", bundle);

I get the output

bundle: NSBundle (not yet loaded)

The 'not yet loaded' part scares me a bit. Why isn't it loaded?

And finally when I try to load my nib with the view controller

ConfigViewController *configViewController = [[ConfigViewController alloc] initWithNibName:@"ConfigViewController" bundle:bundle];

I get the output

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (not yet loaded)' with name 'ConfigViewController.xib'

I've tried both with and without .xib.

Any ideas?

2

2 Answers

1
votes

The load method is used to load executable code like frameworks and these things.
Your bundle doesn't contain any executable file that you want to load, so you don't neel to call [bundle load].
If the name of the bundle ir right, then all you've written except for [bundle load] is fine.Anyways check that the path is the right ones, don't nest too much the instructions:

NSString* path=[ [NSBundle mainBundle] pathForResource: @"Configuration" ofType: @"bundle"];

If this string is the right path, then you are sure that the bundle will be loaded correctly without calling [bundle load].

0
votes

First of all if a bundle is not created properly it will not get loaded. So in-order to create a proper bundle below are the steps for creating a bundle:
1. Add a new target by choosing a templete named bundle under OS X -> Framework & Libraries.

  1. Select newly created target and change BaseSDK from OSX to Latest iOS.

  2. Add .xibs, images or other resources which you want to use it from bundle in Build Phrases -> Copy Bundle Resources.

  3. Add CoreFoundation framework in Build Phrases -> Link binary with Libraries.

  4. Compile the target choosing iOS Device.

  5. Save the newly created bundle from Products directory to some place.

Now copy that bundle into your main project. Load a bundle using below code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];

NSBundle *bundle = [NSBundle bundleWithPath:path];"

You are now set with the new bundle.