1

I am trying to create an IBOutlet for NSWindow in AppDelegate.swift, I am able to create IBOutlets for buttons by control + dragging from storyboard to swift file, but I am not able to do this for NSWindow. How do I do this? Xcode 9.2 Swift 4.0

Thanks

jnpdx
  • 45,847
  • 6
  • 64
  • 94
Dregg
  • 653
  • 6
  • 17

1 Answers1

1

Create a window property:

weak var window: NSWindow?

Main window reference:

 func applicationDidFinishLaunching(_ aNotification: Notification) {
        window = NSApplication.shared.windows.first
    }
RTXGamer
  • 3,215
  • 6
  • 20
  • 29
  • 1
    @Dregg What you need is to use the view controller's view window property. Just make sure to access it on viewWillAppear or any other method other than viewDidLoad which window will always be nil https://stackoverflow.com/a/43426545/2303865 – Leo Dabus Jul 22 '21 at 17:18
  • 1
    @LeoDabus Thanks, that worked as well – Dregg Jul 22 '21 at 17:42
  • @Dregg Make sure to use the view window. It will always return the correct window. It is not necessarily always the first window. – Leo Dabus Jul 22 '21 at 17:46