Class

NSWindow​Controller

An NSWindow​Controller object manages a window, usually a window stored in a nib file.

Overview

This management entails:

  • Loading and displaying the window

  • Closing the window when appropriate

  • Customizing the window’s title

  • Storing the window’s frame (size and location) in the defaults database

  • Cascading the window in relation to other document windows of the application

A window controller can manage a window by itself or as a role player in the Application Kit’s document-based architecture, which also includes NSDocument and NSDocument​Controller objects. In this architecture, a window controller is created and managed by a “document” (an instance of an NSDocument subclass) and, in turn, keeps a reference to the document.

The relationship between a window controller and a nib file is important. Although a window controller can manage a programmatically created window, it usually manages a window in a nib file. The nib file can contain other top-level objects, including other windows, but the window controller’s responsibility is this primary window. The window controller is usually the owner of the nib file, even when it is part of a document-based application. Regardless of who is the file’s owner, the window controller is responsible for freeing all top-level objects in the nib file it loads.

For simple documents—that is, documents with only one nib file containing a window—you need do little directly with NSWindow​Controller. The Application Kit will create one for you. However, if the default window controller is not sufficient, you can create a custom subclass of NSWindow​Controller. For documents with multiple windows or panels, your document must create separate instances of NSWindow​Controller (or of custom subclasses of NSWindow​Controller), one for each window or panel. An example is a CAD application that has different windows for side, top, and front views of drawn objects. What you do in your NSDocument subclass determines whether the default NSWindow​Controller or separately created and configured NSWindow​Controller objects are used.

Subclassing NSWindowController

You should create a subclass of NSWindow​Controller when you want to augment the default behavior, such as to give the window a custom title or to perform some setup tasks before the window is loaded. In your class’s initialization method, be sure to invoke on super either one of the init​With​Window​Nib​Name:​... initializers or the init(window:​) initializer. Which one depends on whether the window object originates in a nib file or is programmatically created.

Three NSWindow​Controller methods are most commonly overridden:

Method Name

Description

window​Will​Load()

Override to perform tasks before the window nib file is loaded.

window​Did​Load()

Override to perform tasks after the window nib file is loaded.

window​Title(for​Document​Display​Name:​)

Override to customize the window title.

You can also override load​Window() to get different nib-searching or nib-loading behavior, although there is usually no need to do this.

Symbols

Initializing Window Controllers

init(window:​ NSWindow?)

Returns a window controller initialized with a given window.

init(window​Nib​Name:​ String)

Returns a window controller initialized with a nib file.

init(window​Nib​Name:​ String, owner:​ Any)

Returns a window controller initialized with a nib file and a specified owner for that nib file.

init(window​Nib​Path:​ String, owner:​ Any)

Returns a window controller initialized with a nib file at an absolute path and a specified owner.

Loading and Displaying the Window

func load​Window()

Loads the receiver’s window from the nib file.

func show​Window(Any?)

Displays the window associated with the receiver.

var is​Window​Loaded:​ Bool

A Boolean value that indicates whether the nib file containing the receiver’s window has been loaded.

var window:​ NSWindow?

The window owned by the receiver.

func window​Did​Load()

Sent after the window owned by the receiver has been loaded.

func window​Will​Load()

Sent before the window owned by the receiver is loaded.

Accessing the Document

var document:​ Any​Object?

The document associated with the window controller.

func set​Document​Edited(Bool)

Sets the document edited flag for the window controller.

Closing the Window

func close()

Closes the window if it was loaded.

var should​Close​Document:​ Bool

A Boolean value that indicates whether the receiver necessarily closes the associated document when the window it manages is closed.

Getting Nib and Storyboard Information

var owner:​ Any​Object

The owner of the nib file containing the window managed by the receiver.

var storyboard:​ NSStoryboard?

The storyboard file from which the window controller was loaded.

var window​Nib​Name:​ String?

The name of the nib file that stores the window associated with the receiver.

var window​Nib​Path:​ String?

The full path of the nib file that stores the window associated with the receiver.

Accessing Window Attributes and Content

var should​Cascade​Windows:​ Bool

A Boolean value that indicates whether the window will cascade in relation to other document windows when it is displayed.

var window​Frame​Autosave​Name:​ String?

The name under which the frame rectangle of the window owned by the receiver is stored in the defaults database.

func synchronize​Window​Title​With​Document​Name()

Synchronizes the displayed window title and the represented filename with the information in the associated document.

func window​Title(for​Document​Display​Name:​ String)

Returns the window title to be used for a given document display name.

var content​View​Controller:​ NSView​Controller?

The view controller for the window’s content view.

func dismiss​Controller(Any?)

Dismisses the window controller.