问题描述:

It's some time Apple has provided us with storyboards, I'm following its' evolution since the very beginning, it has some pros and cons, but for sure this is a "future" if you compare it to the oldschool "xib" files...

What I'm aware of here is something called "External Object". When using standard "xibs" we are always able to drag&drop two special kind of objects from IB object library (right side pane)

"File's Owner" is an example of this "External object".

We were able to drag&drop one more ExternalObject to our xib file and hook it up to some external object by instantiating such ViewController using this UINib API:

- (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil;

... yes it was possible, documentation:

If the nib file contains any proxy objects beyond just the File’s

Owner proxy object, you can specify the runtime replacement objects

for those proxies using the options dictionary. In that dictionary,

add the UINibExternalObjects key and set its value to a dictionary

containing the names of any proxy objects (the keys) and the real

objects to use in their place. The proxy object’s name is the string

you assign to it in the Name field of the Interface Builder inspector

window.

Now when it comes to storyboards - External Objects are not available in the objects inspector. That would make sense because I haven't found any API that would allow us to inject external objects into a view controller created from storyboard. We only have this:

- (id)instantiateInitialViewController;

- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier;

So there is no way to pass our "options".

However...

External Object is not available in storyboard until... you make a Swift project (maybe there is another factor that enables External Object in my storyboard, I'm not sure).

So it is possible to have a setup where storyboards allow us to drag & drop External Object into a storyboard. But how can we then take advantage of this? How can I load a view controller from storyboard and hook up these proxy objects?

  • devforum thread

网友答案:

You can drag&drop an NSObject on a view controllers scene and give it the right class in the inspector.

it can be wired up as expected

I created a Example Project for this question/answer and published it at GitHub.

相关阅读:
Top