OS X - How can a NSViewController find its window?

Indeed, it's self.view.window (Swift).

This may be nil in viewDidLoad() and viewWillAppear(), but is set properly by the time you get to viewDidAppear().


You can use [[self view] window]


One issue with the other answers (i.e., just looking at self.view.window) is that they don't take into account the case that when a view is hidden, its window property will be nil. A view might be hidden for a lot of reasons (for example, it might be in one of the unselected views in a tab view).

The following (swift) extension will provide the windowController for a NSViewController by ascending the view controller hierarchy, from which the window property may then be examined:

public extension NSViewController {
    /// Returns the window controller associated with this view controller
    var windowController: NSWindowController? {
        return ((self.isViewLoaded == false ? nil : self.view)?.window?.windowController)
            ?? self.parent?.windowController // fallback to the parent; hidden views like those in NSTabView don't have a window
    }

}