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文件还是来自代码创建的。
《明日此时》历时一年,这是我翻译的小说,后续时间充裕的话,也会继续给大家翻译技术文档的哦~握爪嘤