I’ve been experimenting a bit with storyboards for the Mac, introduced by Apple at last year’s WWDC (OS X 10.10 Yosemite if you recall). In many regards, storyboards are a hefty improvement over what we had before — especially compared to creating a single giant window controller!
But one thing that really grates for me is how view controllers higher up in the stack go about referencing their children. As on iOS, there are no outlets between controllers, so this has to be accomplished at runtime. I find myself having to write code like this:
The typecasts irk me, and then the hardcoded indexes even more!
There’s an argument to made that perhaps this sort of knowledge between controllers shouldn’t exist. In which case you’re trying to do everything in quite a vague fashion: traversing the view controller hierarchy as generically as possible, and sending messages up the responder chain. It sounds nice, but I haven’t quite managed to achieve it completely yet!
So how does this compare to iOS? It seems to me, I run into problems like the above a little less on iOS. Why?
I think generally, less of the view controller hierarchy is pre-built in the storyboard. Instead, something like a master VC is responsible for creating the detail VC on-demand, and sending it up the hierarchy for a split view controller to display. Perhaps Mac apps should closer attempt to follow this pattern
When using a container view to embed one VC inside another, iOS exposes that relationship as a full-blown segue. -prepareForSegue:sender: is called, and the segue’s identifier can be configured in IB. This is still pretty annoying — and slightly weird — code to write:
but at least there is an identifier available to use!
I also note that a fairly major difference between iOS and OS X is for the Apple-supplied container VCs. On iOS there are various methods exposed to search up the hierarchy and find the nearest container. e.g. any view controller can easily find out the -navigationController that contains it. There’s no such API for the OS X equivalents.
Recently on iOS we’ve gotten some fairly generic UIViewController APIs: -showViewController:sender: and -showDetailViewController:sender:, which remove a lot of the need for finding the correct container to message. I wonder if Apple’s intention is similar for OS X, but rather awkwardly, no-one’s written/exposed those APIs yet!
That’s about it for now. Let’s see what WWDC brings next week :-)