Re: How do I load resources from NSBundle correctly?
Re: How do I load resources from NSBundle correctly?
- Subject: Re: How do I load resources from NSBundle correctly?
- From: Ondra Cada <email@hidden>
- Date: Sat, 24 Nov 2001 16:18:44 +0100
Gore,
>
>>>>> Gore (G) wrote at Sat, 24 Nov 2001 16:46:43 +0200:
G> What should I do to load the resources inside a bundle in a correct
G> way ? here is what I do right now, this code is inside my NSDocument
G> class, used to load the interface for my plugins, but if I open a new
G> document and try to load it again, it gives me the "Error in loading
G> interface." error,
At the first look, the code seems to be all right... I've got no time to
build&test it myself.
G> hmm...please help, newID is a class instance in my
G> NSDocument.
You mean "a property variable", I guess ;)
G> - (IBAction)runPluginWithMenu:(id)sender // sender is a NSMenuItem from
G> the plugin menu
G> {
G> NSBundle *thePlugin = [NSBundle bundleWithPath:
G> [[[NSBundle mainBundle] builtInPlugInsPath]
builtInPlugInsPath is from your NSBundle category?
G> stringByAppendingFormat: @"/%@.bundle", [sender title]]];
G>
G> currentPluginName = [sender title]; // save the name for later use
I'd personally prefer something like
currentPluginName=[sender representedObject];
// - use representedObject (don't forget to set it!), or tag, or something
alike instead of title. Title would change when your app is localized;
// - also, if you are saving it into a variable anyway, do it before it's
used for locating the bundle and just use it:
NSBundle *thePlugin=[NSBundle bundleWithPath:[[NSBundle mainBundle]
pathForResource:currentPluginName ofType:@"bundle"]];
// unless you have strong reasons to go the way of "builtInPlugInsPath", I'd
recommend this standard service
G> if ( runningPlugin == NO ) // just to be safe...so that you can't
G> run many plugins at the same time
One might want to ;))) (of course, _you_ design your app, and _you_ know
whether it might be desirable or not)
Also don't forget that you can load a plugin, but you _can't_ unload it.
G> {
G> if ( [thePlugin load] )
G> {
G> newID = [[[thePlugin principalClass] alloc] init];
Which class this happens to be? It is imperative that it is implemented
inside the plugin...
G> [newID setCurrentDocument: self]; // give the plugin
G> instance the current document to play with
G>
G> if ( [newID pluginIsGUI] ) // if YES, then load the resources
G> {
G> if ( [NSBundle loadNibNamed: @"PluginInterface" owner:
G> newID] ) // should the owner be a class or instance!?
...since this would try to locate the NIB inside "bundleForClass:[newID class]".
And of course, the owner should be an instance, since its properties will be
filled by the appropriate values (as the outlets of File's Owner are bound).
G> {
...
G> }
G> else
G> NSRunAlertPanel( [sender title], @"Error in loading
G> interface.", @"OK", nil, nil );
So, if this problem occurs, I guess
- either the bundle does not contain the NIB;
- or the newID class is not part of the bundle.
Otherwise alas, I dunno...
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc