Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference main NSWindow in AppDelegate using Storyboard?

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.

like image 254
Charlie Avatar asked Jun 07 '15 04:06

Charlie


1 Answers

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

like image 68
Kaunteya Avatar answered Oct 26 '22 21:10

Kaunteya