Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/AppKit.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSWindowController.h |
Related sample code |
An NSWindowController
object manages a window, usually a window stored in a nib file.
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 NSDocumentController
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 NSWindowController
. The Application Kit will create one for you. However, if the default window controller is not sufficient, you can create a custom subclass of NSWindowController
. For documents with multiple windows or panels, your document must create separate instances of NSWindowController
(or of custom subclasses of NSWindowController
), 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 NSWindowController
or separately created and configured NSWindowController
objects are used.
You should create a subclass of NSWindowController
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 initWithWindowNibName:...
initializers or the initWithWindow:
initializer. Which one depends on whether the window object originates in a nib file or is programmatically created.
Three NSWindowController
methods are most commonly overridden:
Method Name |
Description |
---|---|
Override to perform tasks before the window nib file is loaded. |
|
Override to perform tasks after the window nib file is loaded. |
|
Override to customize the window title. |
You can also override loadWindow
to get different nib-searching or nib-loading behavior, although there is usually no need to do this.
– initWithWindow:
– initWithWindowNibName:
– initWithWindowNibName:owner:
– initWithWindowNibPath:owner:
– setShouldCascadeWindows:
– shouldCascadeWindows
– setWindowFrameAutosaveName:
– windowFrameAutosaveName
– synchronizeWindowTitleWithDocumentName
– windowTitleForDocumentDisplayName:
Closes the window if it was loaded.
- (void)close
Because this method closes the window without asking the user for confirmation, you usually do not invoke it when the Close menu command is chosen. Instead invoke NSWindow’s performClose:
on the receiver’s window. See “Window Closing Behavior” for an overview of deallocation behavior when a window is closed.
NSWindowController.h
Returns the document associated with the receiver.
- (id)document
The document associated with the receiver or nil
if there is none.
When a window controller is added to a document's list of window controllers, the document sets the window controller’s document with setDocument:
. The Application Kit uses this outlet to access the document for relevant next-responder messages.
NSWindowController.h
Returns a window controller initialized with a given window.
- (id)initWithWindow:(NSWindow *)window
The window object to manage; can be nil
.
A newly initialized window controller.
This method is the designated initializer for NSWindowController
.
This initializer is useful when a window has been loaded but no window controller is assigned. The default initialization turns on cascading, sets the shouldCloseDocument
flag to NO
, and sets the window frame autosave name to an empty string. As a side effect, the created window controller is added as an observer of the NSWindowWillCloseNotification
s posted by that window object (which is handled by a private method). If you make the window controller a delegate of the window, you can implement NSWindow’s windowShouldClose:
delegate method.
NSWindowController.h
Returns a window controller initialized with a nib file.
- (id)initWithWindowNibName:(NSString *)windowNibName
The name of the nib file (minus the “.nib
” extension) that archives the receiver’s window; cannot be nil
.
Sets the owner of the nib file to the receiver. The default initialization turns on cascading, sets the shouldCloseDocument
flag to NO
, and sets the autosave name for the window’s frame to an empty string.
NSWindowController.h
Returns a window controller initialized with a nib file and a specified owner for that nib file.
- (id)initWithWindowNibName:(NSString *)windowNibName owner:(id)owner
The name of the nib file (minus the “.nib
” extension) that archives the receiver’s window; cannot be nil
.
The nib file's owner; cannot be nil
.
The default initialization turns on cascading, sets the shouldCloseDocument
flag to NO
, and sets the autosave name for the window’s frame to an empty string.
NSWindowController.h
Returns a window controller initialized with a nib file at an absolute path and a specified owner.
- (id)initWithWindowNibPath:(NSString *)windowNibPath owner:(id)owner
The full path to the nib file that archives the receiver’s window; cannot be nil
.
The nib file's owner; cannot be nil
.
Use this method if your nib file is at a fixed location (which is not inside either the file’s owner’s class’s bundle or in the application’s main bundle). The default initialization turns on cascading, sets the shouldCloseDocument
flag to NO
, and sets the autosave name for the window’s frame to an empty string.
NSWindowController.h
Returns whether the nib file containing the receiver’s window has been loaded.
- (BOOL)isWindowLoaded
YES
if the nib file containing the receiver’s window has been loaded, NO
otherwise.
NSWindowController.h
Loads the receiver’s window from the nib file.
- (void)loadWindow
You should never directly invoke this method. Instead, invoke window
so the windowDidLoad
and windowWillLoad
methods are invoked. Subclasses can override this method if the way it finds and loads the window is not adequate. It uses NSBundle’s bundleForClass:
method to get the bundle, using the class of the nib file owner as argument. It then locates the nib file within the bundle and, if successful, loads it; if unsuccessful, it tries to find the nib file in the main bundle.
NSWindowController.h
Returns the owner of the nib file containing the window managed by the receiver.
- (id)owner
The owner of the nib file containing the window managed by the receiver; usually self
, but can be the receiver’s document or some other object.
NSWindowController.h
Sets the document associated with the window managed by the receiver.
- (void)setDocument:(NSDocument *)document
The new document.
Documents automatically call this method when they add a window controller to their list of window controllers; you should not call it directly.
NSWindowController.h
Sets the document edited flag for the window controller.
- (void)setDocumentEdited:(BOOL)flag
YES
if the document has been edited since its last save, NO
if it hasn't.
The window controller uses this flag to control whether its associated window shows up as dirty. You should not call this method directly for window controllers with an associated document; the document calls this method on its window controllers as needed.
NSWindowController.h
Sets whether the window should cascade in relation to other document windows.
- (void)setShouldCascadeWindows:(BOOL)flag
YES
if the window should cascade in relation to other document windows, NO
otherwise.
Cascading in relation to other document windows means having a slightly offset location so that the title bars of previously displayed windows are still visible.
The default is YES
.
NSWindowController.h
Sets whether the receiver should necessarily close the associated document when the window it manages is closed.
- (void)setShouldCloseDocument:(BOOL)flag
YES
if the receiver necessarily closes the associated document when the window it manages is closed, NO
otherwise.
If NO
, the document is closed only when the last remaining window of the document is closed.
The default is NO
.
NSWindowController.h
Sets the window controller’s window.
- (void)setWindow:(NSWindow *)aWindow
The new window.
This method releases the old window and any associated top-level objects in its nib file and assumes ownership of the new window. You should generally create a new window controller for a new window and release the old window controller instead of using this method.
NSWindowController.h
Sets the name under which the window’s frame is saved in the defaults database.
- (void)setWindowFrameAutosaveName:(NSString *)name
The name under which the window’s frame is saved in the defaults database.
By default, name is an empty string, causing no information to be stored in the defaults database.
– windowFrameAutosaveName
– setFrameAutosaveName:
(NSWindow)NSWindowController.h
Returns whether the window will cascade in relation to other document windows when it is displayed.
- (BOOL)shouldCascadeWindows
YES
if the window will cascade in relation to other document windows, NO
otherwise.
The default is YES
.
NSWindowController.h
Returns whether the receiver necessarily closes the associated document when the window it manages is closed.
- (BOOL)shouldCloseDocument
YES
if the receiver necessarily closes the associated document when the window it manages is closed, NO
otherwise.
If NO
, the document is closed only when the last remaining window of the document is closed.
The default is NO
.
NSWindowController.h
Displays the window associated with the receiver.
- (IBAction)showWindow:(id)sender
The control sending the message; can be nil.
If the window is an NSPanel
object and has its becomesKeyOnlyIfNeeded
flag set to YES
, the window is displayed in front of all other windows but is not made key; otherwise it is displayed in front and is made key. This method is useful for menu actions.
– makeKeyAndOrderFront:
(NSWindow)– orderFront:
(NSWindow)NSWindowController.h
Synchronizes the displayed window title and the represented filename with the information in the associated document.
- (void)synchronizeWindowTitleWithDocumentName
Does nothing if the window controller has no associated document or loaded window. This method queries the window controller’s document to get the document’s display name and full filename path, then calls windowTitleForDocumentDisplayName:
to get the display name to show in the window title.
NSWindowController.h
Returns the window owned by the receiver.
- (NSWindow *)window
The window owned by the receiver or nil
if there isn’t one.
If the window has not yet been loaded, this method attempts to load the window’s nib file using loadWindow
. Before it loads the window, it invokes windowWillLoad
, and if the window controller has a document, it invokes the document's corresponding method windowControllerWillLoadNib:
(if implemented). After loading the window, this method invokes windowDidLoad
and, if there is a document, the NSDocument method windowControllerDidLoadNib:
(if implemented).
– windowControllerWillLoadNib:
(NSDocument)NSWindowController.h
Sent after the window owned by the receiver has been loaded.
- (void)windowDidLoad
The default implementation does nothing.
NSWindowController.h
Returns the name under which the frame rectangle of the window owned by the receiver is stored in the defaults database.
- (NSString *)windowFrameAutosaveName
The name under which the frame rectangle of the window owned by the receiver is stored in the defaults database.
NSWindowController.h
Returns the name of the nib file that stores the window associated with the receiver.
- (NSString *)windowNibName
The name of the nib file that stores the window associated with the receiver.
If initWithWindowNibPath:owner:
was used to initialize the instance, windowNibName
returns the last path component with the “.nib
” extension stripped off. If initWithWindowNibName:
or initWithWindowNibName:owner:
was used, windowNibName
returns the name without the “.nib
” extension.
NSWindowController.h
Returns the full path of the nib file that stores the window associated with the receiver.
- (NSString *)windowNibPath
The full path of the nib file that stores the window associated with the receiver; nil
if it cannot be located.
If initWithWindowNibPath:owner:
was used to initialize the instance, the path is just returned. If initWithWindowNibName:
or initWithWindowNibName:owner:
was used, windowNibPath
locates the nib in the file’s owner’s class’ bundle or in the application’s main bundle and returns the full path (or nil
if it cannot be located). Subclasses can override this to augment the search behavior, but probably ought to call super
first.
NSWindowController.h
Returns the window title to be used for a given document display name.
- (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName
The display name for the document. This is the last path component under which the document file is saved.
The default implementation returns displayName. Subclasses can override this method to customize the window title. For example, a CAD application could append “-Top” or “-Side,” depending on the view displayed by the window.
NSWindowController.h
Sent before the window owned by the receiver is loaded.
- (void)windowWillLoad
The default implementation does nothing.
NSWindowController.h
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-05-23)