More on Identification within Mac Storyboards

Well, WWDC has come and gone, and there seem to have been some nice improvements to storyboards this year, particularly storyboard references. But I’ve found nothing to address my gripe from last month, that locating child view controllers on the Mac is pretty awful. So where do we stand?

For a moment, I got very excited, and thought I’d been a complete idiot. I’d overlooked that NSViewController conforms to the NSUserInterfaceItemIdentification protocol, which gives us an .identifier @property. Ah ha! You can specify that in Interface Builder as the “Restoration Identifier”, and then at runtime be a bit more precise with your code:

It’s not bad. I do worry a bit though, that this could be considered abuse of the “restoration” identifier, and might have unintended consequences which I’ve not stumbled upon yet.

More problematically though, that identifier is expected to be unique within a window. If you’re constructing a sort of generic piece of your UI, featuring the same view controller more than once (and I am), you’re breaking the rules, and IB likely complains at you.

Where I ran into this, I was writing a fairly dumb container view controller, whose job is mostly just to add a smattering of chrome around a child VC. I’ve come up with a similar solution as above, but this time using the container view for identification purposes:

Here, containerView is an outlet I’ve wired up in IB to the view whose job is to embed the child view controller. After loading, it seems perfectly reasonable to me to use it to identify the controller whose view it embeds.

For added kicks, my friend Daniel Tull showed me an approach he’s taking, identifying a child VC purely by its class. Personally I find it a little dubious, as will fall down if you have more than one child of a particular type, but otherwise it works just fine.

So there you have it, four different ways to locate child view controllers from a storyboard, none of which I’m particularly happy with!

  1. By index
  2. By restoration identifier
  3. By containing view
  4. By class
© Mike Abdullah 2007-2015