The Chain of Responsibility pattern is designed to decouple a message's sender from the receiver. A message or event is passed down a linked list of objects until one of them handles the message,giving multiple objects the opportunity to handle or ignore the message.
In Cocoa, all objects that respond to user interface input are subclasses of the abstract class NSResponder.
As a user works with an application, Cocoa automatically tracks where the user’s focus is.The window currently receiving keyboard input is known as the “key” window.The currently focused document is known as the “main” window. The application object tracks both the key and main windows. References to the current key and main NSWindow objects can be obtained from an NSApplication instance with the -keyWindow and -mainWindowmessages, respectively.
The currently focused view is known as the first responder.This is the start of the Responder Chain.To obtain a reference to the object that is currently a window’s first responder, send the -firstResponder message to the window object.
In this fashion, each window has its own Responder Chain.As the user changes focus from one view to another within a window, the Responder Chain for that window is updated.To change focus, an object is first asked if it is willing to become the next responder via the -acceptsFirstResponder message.Typically only views that can handle
keyboard input will say YES. If so, the current first responder is asked to relinquish its first responder status by being sent the -resignFirstResponder message. Sometimes, in the case of text fields that validate input, the answer might be NO until valid data is entered. Finally, the object to become the new first responder is sent the -becomeFirstResponder message.
Putting this all together, the extended Responder Chain used by Cocoa’s Target Action mechanism is as follows:
1. Start with the first responder of the key window.
2. Follow the Responder Chain up the view hierarchy.
3. Try the window object.
4. Try the window’s delegate, which is often an NSDocument instance.
5. Next is an NSWindowController instance, if there is one.
6. Repeat 1-5 starting with the first responder of the main window.
7. Try the NSApplication object and its delegate.
8. Try the NSDocumentController, if there is one.
While learning about the Responder Chain, sometimes it is helpful to see what objects are actually in the chain at a given moment and to see how application context changes what objects are present.
Every UIResponder has a pointer called nextResponder, and together these objects make up the responder chain. A touch event starts at the view that was touched. The nextResponder of a view is typicaly its UIViewController(if it has one) or its superview. The nextResponder of a view controller is typically its view's superview. The top-most superview is the window(UIWindow inherits from UIView). The window's nextResponder is the singleton instance of UIApplication. If the application doesn't handle the event, then it is discarded.