Responder Chain

本文深入解析了 Cocoa 中的 Responder Chain 机制,解释了如何通过该机制实现用户界面输入的响应与传递。从第一响应者到应用委托,详细介绍了 Responder Chain 的构成与工作流程。

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.

转载于:https://www.cnblogs.com/grep/archive/2012/06/20/2556262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值