Re: Safe cross references between scenes in an OS X storyboard
Re: Safe cross references between scenes in an OS X storyboard
- Subject: Re: Safe cross references between scenes in an OS X storyboard
- From: Quincey Morris <email@hidden>
- Date: Wed, 09 Mar 2016 10:00:59 -0800
- Feedback-id: 167118m:167118agrif8a:167118sJH2UrsytG:SMTPCORP
On Mar 9, 2016, at 05:59 , Bill Cheeseman <email@hidden> wrote:
>
> But the main thrust of my question was whether this is a safe and sensible way to crossreference other storyboard scenes -- at least when prepareForSegue(_:sender:) isn't available, as it isn't here. Having thought about it for a while now, I guess it is a sensible approach. It has less to do with storyboards than with the longstanding fact that NSApplication has a 'mainwindow' property and NSWindow has a 'contentViewController' property.
I think you ended up solving the “wrong” problem. The lazy initialization is unnecessary, and so is the mucking about with optionals, because the window controller and window both exist by the time applicationDidFinishLaunching is invoked. The window isn’t main yet, but that’s the wrong place to look for it: apart from anything else, your proposed code is fragile because if your app ever has other windows that become main, even temporarily, you temporarily lose the ability to find your shoebox window.
(I’m talking about window controllers because, once you have a reference to the window controller, it’s easy to find the content view controller.)
The storyboard is automatically instantiated in NSApplicationMain. (When the instantiation happens, there must be a strong reference to the WC stored somewhere, otherwise the WC would be deallocated, but it looks like you don’t get access to that reference in Swift projects. In Obj-C, you used to get a templated property on the app delegate.) What you can do instead is subclass NSWindowController, and use ‘windowDidLoad’ as your opportunity to save your own strong reference to the WC. (In Swift, I think I’d make it a static property of the WC class. This also lets you check that nothing creates a second main window.)
If you find this inconvenient or distasteful, your other alternative is to take the “isInitialController” status off the WC in the storyboard, and instantiate the WC yourself in (say) applicationDidFinishLaunching. Or, almost the same thing, move the window and view scenes out of the main storyboard into a separate storyboard.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden