开发者

NSBundle loading a NSViewController

开发者 https://www.devze.com 2023-01-01 01:52 出处:网络
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...

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 开发者_StackOverflow社区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!


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];


"View.xib is a nib"? Either it is a xib or it is a nib. A xib is not a nib. xib files can only be used in the interface builder, in deployment applications they must be transformed to nib files. That is what ibtool is doing. Xcode does not simply copy your xib files, it runs them through ibtool that converts them to nib files that are then placed into the Resources folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消