NSWindowController
?An NSWindowController object manages a window, usually a window stored in a nib file.
一個NSWindowController對象管理一個窗口,通常這個窗口存儲在nib文件中。
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 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.
一個窗口控制器管理自身的一個窗口,或者在Application Kit基于文檔的結構中起到一定作用,窗口控制器也包含NSDocument類和NSDocumentController類。在這樣一種結構里,創建一個窗口控制器,并通過一個‘文檔’(它是NSDocument子類的一個實例)來管理,讓它和文檔保持關連性。
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.
窗口控制器與nib文件之間的有著很重要的關系。窗口控制器從程序上來講,雖然能管理被創建的窗口,但它通常是管理nib文件里的這個窗口。這個nib文件可以包含其它高級對象,包括其它窗口,但是窗口控制器的職責是負責當前的窗口。這個窗口控制器通常是nib文件的擁有者,它也是一個基于文件的應用的一部分。暫不管這個文件到底屬于誰,窗口控制器主要負責釋放nib文件加載的所有高級對象。
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.
對于一個簡單的文檔——即一個帶窗口的nib文件——你不需要直接做任何與 NSWindowController 相關的事情, Application Kit包會自為你創建一個窗口控制器。但是,如果默認的窗口控制器不夠用的話,你可以創建一個自定義的 NSWindowController 的子類。當文件帶有多個窗口或面板時,必須單獨創建 NSWindowController 的實例,每一個實例對應一個窗口或一個面板。例如CAD這個軟件,它有不同的窗口,位于邊上的,頂部的,可以拖拽的,以及前端視圖等。你在 NSDocument 子類里的操作決定了是使用默認的 NSWindowController 對象,還是使用單獨創建配置的 NSWindowController 對象。
Subclassing NSWindowController
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.
當需要修改默認的參數動作時,可以創建一個 NSWindowController 的子類,例如,在窗口加載之前,自定義窗口的標題,修改一些啟動任務。在自定義的類的初始化方法里,首先要確定調用了父類的方法,或者使用了“initWithWindowNibName: ”或 “initWithWindow: ”等初始化方法。具體采用哪一種方法,取決于這個窗口對象是來自nib文件還是來自代碼創建的。