Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSBundle loading a NSViewController

I'm looking into a project that would upload custom NSBundles that include a NSViewController. In my main program I've got this code to deal with the bundle after it's been loaded...

id principalClass = [loadedBundle principalClass];
id instance = [[principalClass alloc] init];
[localLabel setStringValue:[instance name]];
NSView *incomingView = [[instance viewController] view];
[localView addSubview:incomingView];
[localView display];

And the principal classes init method in the bundle looks like this...

-(id) init {
    if(self = [super init]){
        name = @"My Plugin";
        viewController = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
    }
    return self;
}

View.nib is a nib located in the bundles project. But whenever I load the bundle I get this error...

2010-05-27 09:11:18.423 PluginLoader[45032:a0f] unable to find nib named: View in bundle path: (null) 2010-05-27 09:11:18.424 PluginLoader[45032:a0f] -[NSViewController loadView] could not load the "View" nib.

I know I've got everything wired up because the line [label setStringValue:[instance name]]; sets the label text correctly. Plus, if I take all of the clases in the bundle and load them into my main applications project everything works as expect. Any thoughts on how I can correctly reference "View" in my bundle?

Thanks!

like image 612
Staros Avatar asked Jun 02 '26 18:06

Staros


1 Answers

In the init method, you shouldn't pass nil to the bundle parameter. According to the UIViewController documentation, passing nil will look up the NIB file in the main bundle (the application's one), which is not what you want.

You can workaround, by using a specialized initializer like this:

- (id) initWithBundle:(NSBundle *)bundle {
    if(self = [super init]){
        name = @"My Plugin";
        viewController = [[ViewController alloc] initWithNibName:@"View" bundle:bundle];
    }
    return self;
}

And use it as follow:

Class principalClass = [loadedBundle principalClass];
id instance = [[principalClass alloc] initWithBundle:loadedBundle];
like image 119
Laurent Etiemble Avatar answered Jun 04 '26 12:06

Laurent Etiemble