Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

enter image description here

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 without using a singleton.)

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

like image 800
zoul Avatar asked Mar 17 '11 14:03

zoul


2 Answers

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.

like image 81
hooleyhoop Avatar answered Nov 15 '22 07:11

hooleyhoop


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

like image 24
antonsergeev88 Avatar answered Nov 15 '22 09:11

antonsergeev88