On Mar 9, 2016, at 05:59 , Bill Cheeseman <wjcheese...@gmail.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to