| < Previous PageNext Page > |
Objects of the core application classes—NSApplication, NSWindow, and NSView—are responders. They are instances of classes that inherit, directly or indirectly, from NSResponder (see Figure 6-19). This abstract class defines the interface and expected behavior of objects that can respond to events. The subclasses of NSResponder implement this behavior in whole or in part.
Note: Two other Application Kit classes directly inherit from NSResponder: NSWindowController and NSDrawer. Although NSWindowController is part of the Cocoa document architecture (described in “Other Cocoa Architectures”), neither of these classes is central to the core Cocoa application architecture.
The NSResponder class defines an interface for three major patterns or mechanisms of the core application architecture:
It declares a number of methods for handling event messages (that is, messages originating in user events such as mouse clicks and key presses).
It declares dozens of methods for handling action messages that are tied to the standard key bindings (for example, those that move the insertion point within text). An action message is dispatched to a target object; if no target is specified, the application searches for a suitable responder.
It defines a set of methods for designating and managing the responders in an application. These responders form what as known as a responder chain—a series of responders along which an event or action message is passed until it finds an object capable of handling it.
The responder chain is a central mechanism in the Application Kit's architecture for event handling. It is a linked series of responder objects along which an event or action message is passed. As depicted in Figure 6-20, if a responder object cannot handle an event or action—that is, it either does not respond to the message or the event is one it doesn't recognize—it resends the message to the next responder in the chain. The message travels up the chain, toward higher-level objects, until it is handled. (If it isn't handled, it is discarded.)
When the Application Kit constructs the objects in a application, it sets up the responder chain for each window. The essential objects in a responder chain are the NSWindow object and its view hierarchy. Lower views in the hierarchy are given the chance to handle an event or action message before higher level objects. The NSWindow keeps a reference to the first responder, which is usually the currently selected view in the window, and gives it first shot at responding to the message. For event messages, the responder chain usually ends at the NSWindow object managing the window in which the event occurred, although other next-responders can be added after the NSWindow object.
For action messages, the responder chain is more complex. Two factors determine the chain of responders for action messages:
If the application currently has both a main window and a key window, the responders chains of both windows are involved, with the key window's responder chain first given the chance at handling the action. At the end of each window's chain, the NSWindow delegate is given a chance to respond; at the end of the combined responder chains are NSApp and its delegate.
The type of application—simple, document-based, or an application that uses a window controller—determines the type and location of responder objects in the chain.
The NSResponder class also includes declarations of methods for error presentation and recovery, message dispatch, application help, and other features.
Further Reading: To learn more about responders and the responder chain, see "About the Responder Chain."
| < Previous PageNext Page > |
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-12-20)
|