views:

182

answers:

2

Let me preface this by saying that I'm a very raw iPhone developer.

I'm making a static library that can be used in iPhone apps. It needs to show a view but static libraries, as I understand it, cannot include nibs (or xibs, in my case). So, I have created a separate bundle containing the xib I need. I then include the bundle along with the static library in the app. I want to initialize a class that extends UIViewController using the xib in question.

I have the following code in my library:

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Mobtest" ofType:@"bundle"];
NSBundle *mobtestBundle = [NSBundle bundleWithPath:bundlePath];
NSLog([@"bundle path: " stringByAppendingString: [[NSBundle bundleWithPath:bundlePath] bundlePath]]);
sharedInstance = [[MTFeedbackViewController alloc] initWithNibName:@"MTFeedbackViewController" bundle:mobtestBundle];

The bundle path outputted by NSLog is the correct path for the bundle in the compiled app. Unfortunately, I cannot initialize the UIViewController and instead get the following error:

2010-08-20 22:08:16.102 MobtestLibSampleApp[2332:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "MTFeedbackViewController"'

Does anyone have any suggestions of why the xib cannot be loaded? Perhaps something in it is linked incorrectly? Is there any place where I can get additional debugging information?

A: 

I hope you actually mean nib. xibs are an XML serialization for editing by Interface Builder; they're compiled to xibs during the build process. iPhone nibs appear to be a binary plist (I don't think binary plists existed before about 10.3, I don't know what happened before that).

Try NSLog(@"%@",[mobtestBundle pathForResource:@"MTFeedbackViewController" ofType:@"nib"]).

Also note that the first argument to NSLog is a printf-style format string, so you invoke undefined behaviour if your bundle path contains a %.

tc.
I was under the impression that it can actually load xibs also, but I went ahead and just implemented my view in code.
pr1001
It loads nibs, not xibs ;)
tc.
A: 

Have you considered ditching the nib and just building your view in code by implementing -loadView in your view controller? If you don't have many views, and/or they aren't complicated, this would be easier for users of your library, since they wouldn't have to worry about including a bundle along with your lib.

For debugging purposes, you can override -loadView in your view controller and inspect self.nibName and self.nibBundle to make sure that your controller is getting the resources that you're passing in. Just remember to call [super loadView] so that your nib actually gets loaded. (Note that if you override loadView to build your view, and set the view property of your controller, you should not call [super loadView].)

The docs say that the nib inside your bundle should live in the "lang specific project dir" or in the "Resources" sub dir. Double-check your bundle target in Xcode to make sure that your nib is being copied into the correct place (either the 'Projects' or 'Resources' subdir) in your bundle. Also, try inspecting the contents of your bundle in the loadView method:

NSLog(@"nib path inside bundle: %@", [self.nibBundle pathForResource:@"MTFeedbackViewController" ofType:@"nib"]);
Brian Chapados
Thanks, Brian, that's what I ended up doing.
pr1001