daven11 daven11 - 3 months ago 14
Swift Question

Can't Connect window to IBOutlet with storyboard

I'm trying to access the Window in the AppDelegate. If I make a new OSX Cocoa project without storyboards, then the AppDelegate contains a window variable like so

class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!


I can delete the connection between the outlet and the window and reconnect it over and over again and it works fine.

Now if I create a new OSX Cocoa project with storyboards and add the window variable to the AppDelegate and try to control drag the outlet to the window the same as I did on the project without story boards it doesn't work.

Any suggestions?

(OSX 10.11.5 Xcode 7.3.1 (and 8 beta 6) swift 2.2 (and 3))

I know I can do it in code just don't know why I can't connect the outlet.

Answer

Storyboards are composed of scenes, and each scene can be thought of as a single xib file. You can't make connections (actions, outlets, etc) between objects residing in separate xibs, and - analogously - you can't make connections between objects residing in separate scenes in a storyboard.

In the default storyboard setup, your AppDelegate object and the window object are housed in separate scenes - hence why you can't create the outlet:

enter image description here

By contrast, in a xib app both objects reside in the same xib file so creating a connection is possible.