Core Application objects-NSApplication
,NSWindow
, AndNSView
-They are all responders. They areNSResponder
(See Figure 6-19. This abstract class defines the interfaces and expected behaviors of objects that can respond to events.NSResponder
Or partially implement this behavior.
Figure 6-19Nsresponder and its direct subclass
Note:There are two other Application Kit classes that are directly inherited fromNSResponder
:NSWindowController
AndNSDrawer
. AlthoughNSWindowController
Is part of the cocoa document architecture (see descriptions in "other cocoa architectures"). Both classes are not at the center of the core cocoa application architecture.
NSResponder
Class defines an interface for the three main models or mechanisms of the core application architecture:
It declares some processingEvent message(That is, messages originating from user events, such as events such as mouse clicks or press keys.
It declares dozens of processesAction MessageThey are closely related to standard key bindings (such as those that move insertion points inside the text. Action messages are distributed to the target object. If the target is not specified, the application is responsible for retrieving the appropriate response.
It defines a set of methods for assigning and managing contacts in an application. These responders make up what we knowResponder chain-That is, a series of responders, and event or action messages are transmitted between them until the objects that can be processed are found.
The responder chain is the central mechanism of the Application Kit event processing architecture. It is composed of a series of connected responder objects. event or action messages can be transmitted along these objects. As shown in Figure 6-20, if a responder object cannot process an event or action-that is, it does not respond to that message or knows that event, then the message is resent to the next responder in the chain. The message is passed up along the responder chain to a higher level object until it is finally processed (if it is still not processed, it will be discarded ).
Figure 6-20Responder chain
When the Application Kit constructs an object in the application, it establishes a responder chain for each window. The basic object in the responder chain isNSWindow
Object and its view level. A view with a lower level gives priority to event processing or action messages than a view with a higher level.NSWindow
Warranty has oneFirst responderIt is usually the selected view in the current window, the window usually gives it the opportunity to respond to the message first. For event messages, the responder links toNSWindow
Object as the end, although other objects can be added as the next ResponderNSWindow
Object.
For action messages, the response chain is more complex. Two factors determine the response link of an action message:
If the application currently has both the main window and the keyboard focus window, the responder chain of both windows will participate. The responder chain of the keyboard focus window will first get the opportunity to process the action. At the end of each window chain, cocoa willNSWindow
The opportunity for the delegate object to respond to the action; at the end of the merged responder chain isNSApp
And its delegate object.
Application Type-whether it is a simple document-based program or a program using a window controller-This determines the type and location of the responder object in the chain.
NSResponder
Class also contains methods for error representation and recovery, message distribution, application help, and other functions.
= ======================================
The above is Apple's official documentation. The address is:
Http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CoreAppArchitecture/chapter_7_section_6.html
Pay attention to the following points for understanding:
1. the above document describes the logic of the responder and the responder chain in Mac OS X. This is basically the same as the logic in iOS, but the implementation class is different.
2. nsapplication, nswindow, and nsview correspond to uiapplication, uiwindow, and uiview in IOS respectively.
3. When the Application Kit constructs an object in the application, it establishes a responder chain for each window. In iOS, It also corresponds to the uikit framework, the iPhone application has only one window, so there is only one response
User chain.