开发者

Can I use Interface Builder to inject dependencies across multiple nibs?

I am used to supplying the dependencies for my objects from the outside. Interface Builder is a tool that helps doing this, but I can’t make it work with multiple nibs. As soon as I break the main nib into more files, I can no longer make connections between objects in different nibs. Using File Owner does not help very much, since it only allows me to pass one single object into a nib.

Example:

Can I use Interface Builder to inject dependencies across multiple nibs?

Here A and B are some higher-level objects and C and D some kind of lower-level services. As long as all objects are inside one big nib (first picture), everything is fine. But when I split the nib to separate A and B, I have trouble connecting them to C and D. (Obviously I do not want to create two instances of C and D, I want both A and B talking to the same C and D开发者_如何学C without using a singleton.)

Is it possible to do this in Interface Builder? How?


The Objects in your .nib correspond to your view layer, right? When unarchived they are owned by your chosen controller, which mediates between your services and your view.

It would be really unconventional to have any kind of lower-level services, or any kind of dependencies at all archived in your .nib file.


You should use External Objects. In your xib add external object. You can find it in object library. Use it as ordinary object (outlets and so on). Since there's a external object, it will not be created through unarchiving process, you should create yourself. Then you load your nib programmatically:

NSArray*    topLevelObjs = nil;
NSDictionary*    proxies = [NSDictionary dictionaryWithObject:self forKey:@"AppDelegate"];
NSDictionary*    options = [NSDictionary dictionaryWithObject:proxies forKey:UINibExternalObjects];
topLevelObjs = [[NSBundle mainBundle] loadNibNamed:@"Main" owner:self options:options];

More about nib files: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW24

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜