Cocoa - loadNibNamed:owner:topLevelObjects: from loaded bundle

后端 未结 1 1017
-上瘾入骨i
-上瘾入骨i 2021-01-14 19:24

In a document based Cocoa app, I am instantiating several objects (plugins) from external bundles using:

- (NSMutableArray *)getPluginsOfType:(Class)type;
{
         


        
1条回答
  •  梦毁少年i
    2021-01-14 20:05

    It is not clear to me exactly what you're asking - where is the "loading class" that is not in your application's bundle coming from, and what exactly do you mean by "loading class"?

    Maybe the following will help:

    loadNibNamed:owner:topLevelObjects: is an instance method and loads from the bundle the instance represents.

    In your sample the instance you've used you obtain via [NSBundle mainBundle], so the nib is loaded from the applications main bundle.

    There is no "current bundle" concept, but you can obtain an NSBundle instance representing other bundles, e.g. with NSBundle's class method bundleWithURL. So to load a nib from a different bundle first create an NSURL that references the bundle, then create an NSBundle based on that, and finally load the nib.

    HTH

    Addendum - After Question Updated

    From the deprecated +loadNibName:owner: method's description of owner:

    If the class of this object has an associated bundle, that bundle is searched for the specified nib file; otherwise, this method looks in the main bundle.

    This is what you need to replicate when using -loadNibNamed:owner:topLevelObjects:. The method you need is NSBundle's bundleForClass: which returns the NSBundle object that dynamically loaded a class.

    So in your Plugin class you should be able to find the bundle it was loaded from with [NSBundle bundleForClass:[self class]] and then call -loadNibNamed:owner:topLevelObjects: on that.

    0 讨论(0)
提交回复
热议问题