Reference main NSWindow in AppDelegate using Storyboard?

3.5k Views Asked by At

I am attempting to set an outlet for the main window of my app in my App Delegate, then connect it within Interface Builder. I create the outlet within my App Delegate easily:

@IBOutlet weak var mainWindow: NSWindow!

However there's no way, within interface builder, for me to connect a referencing outlet to the App Delegate. Instead, I can only connect it to the Window Controller, which I hope this picture shows:

enter image description here

The first object is the Window Controller and the second object is the First Responder, however the App Delegate object is missing. The menubar has the App Delegate object:

enter image description here

And I can connect anything from the menubar to any outlets in the App Delegate.

I figure I can access the window object by using:

NSApp.windows[0]

But that seems prone to error, especially if I have more than one window.

1

There are 1 best solutions below

0
On BEST ANSWER

I dont know if this is correct way of doing, but this will solve your problem.

Decalre a NSWindow property in AppDelegate

weak var window: NSWindow!

and set the property in something like windowWillLoad of the NSWindowController

(NSApplication.sharedApplication().delegate as! AppDelegate).window = self.window

You will have to subclass NSWindowController to define windowWillLoad