概述 Apps receive and handle events using responder objects . A responder object is any instance of the UIResponder
class, and common subclasses include UIView
, UIViewController
, and UIApplication
. Responders receive the raw event data and must either handle the event or forward it to another responder object. When your app receives an event, UIKit automatically directs that event to the most appropriate responder object, known as the first responder .
Unhandled events are passed from responder to responder in the active responder chain , which is the dynamic configuration of your app’s responder objects. Figure 1 shows the responders in an app whose interface contains a label, a text field, a button, and two background views. The diagram also shows how events move from one responder to the next, following the responder chain.
If the text field does not handle an event, UIKit sends the event to the text field’s parent UIView object, followed by the root view of the window. From the root view, the responder chain diverts to the owning view controller before directing the event to the window. If the window cannot handle the event, UIKit delivers the event to the UIApplication object, and possibly to the app delegate if that object is an instance of UIResponder and not already part of the responder chain.
基于ResponderChain实现对象交互 我们可以借用responder chain实现了一个自己的事件传递链。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #import <UIKit/UIKit.h> @interface UIResponder (Router )- (void )routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo; @end #import "UIResponder+Router.h" @implementation UIResponder (Router )- (void )routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo { [[self nextResponder] routerEventWithName:eventName userInfo:userInfo]; } @end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #import <Foundation/Foundation.h> @interface NSObject (Invocation )- (NSInvocation *)createInvocationWithSelector:(SEL)aSelector; @end #import "NSObject+Invocation.h" @implementation NSObject (Invocation )- (NSInvocation *)createInvocationWithSelector:(SEL)aSelector { NSMethodSignature *signature = [[self class ] instanceMethodSignatureForSelector:aSelector]; if (signature==nil ) { NSString *info = [NSString stringWithFormat:@"-[%@ %@]:unrecognized selector sent to instance" ,[self class ],NSStringFromSelector (aSelector)]; @throw [[NSException alloc] initWithName:@"方法没有" reason:info userInfo:nil ]; return nil ; } NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; invocation.target = self ; invocation.selector = aSelector; return invocation; } @end
在需要响应事件的类中重载routerEventWithName::
方法
1 2 3 - (void )routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo { [self .eventProxy handleEvent:eventName userInfo:userInfo]; }
使用EventProxy
类来专门处理对应的事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #import <Foundation/Foundation.h> @interface EventProxy : NSObject - (void )handleEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo; @end #import "EventProxy.h" #import "ResponderChainDefine.h" #import "UIResponder+Router.h" #import "NSObject+Invocation.h" @interface EventProxy ()@property (nonatomic , strong ) NSDictionary *eventStrategy;@end @implementation EventProxy - (void )handleEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo { NSInvocation *invocation = self .eventStrategy[eventName]; [invocation setArgument:&userInfo atIndex:2 ]; [invocation invoke]; } - (void )cellLeftButtonClick:(NSDictionary *)userInfo { NSIndexPath *indexPath = userInfo[@"indexPath" ]; NSLog (@"indexPath:section:%ld, row:%ld 左边按钮被点击啦!" ,indexPath.section, indexPath.row); } - (void )cellMiddleButtonClick:(NSDictionary *)userInfo { NSIndexPath *indexPath = userInfo[@"indexPath" ]; NSLog (@"indexPath:section:%ld, row:%ld 中间按钮被点击啦!" ,indexPath.section, indexPath.row); } - (void )cellRightButtonClick:(NSDictionary *)userInfo { NSIndexPath *indexPath = userInfo[@"indexPath" ]; NSLog (@"indexPath:section:%ld, row:%ld 右边按钮被点击啦!" ,indexPath.section, indexPath.row); } #pragma mark - getter & setter - (NSDictionary <NSString *, NSInvocation *>*)eventStrategy { if (!_eventStrategy) { _eventStrategy = @{ kTableViewCellEventTappedLeftButton:[self createInvocationWithSelector:@selector (cellLeftButtonClick:)], kTableViewCellEventTappedMiddleButton:[self createInvocationWithSelector:@selector (cellMiddleButtonClick:)], kTableViewCellEventTappedRightButton:[self createInvocationWithSelector:@selector (cellRightButtonClick:)] }; } return _eventStrategy; } @end
在TableViewCell
的事件中,调用routerEventWithName:userInfo:
方法,就会调用到EventProxy
类中的方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @implementation TableViewCell - (IBAction )leftButtonClick:(UIButton *)sender { [self routerEventWithName:kTableViewCellEventTappedLeftButton userInfo:@{@"indexPath" :self .indexPath}]; } - (IBAction )middelButtonClick:(UIButton *)sender { [self routerEventWithName:kTableViewCellEventTappedMiddleButton userInfo:@{@"indexPath" :self .indexPath}]; } - (IBAction )rightButtonClick:(UIButton *)sender { [self routerEventWithName:kTableViewCellEventTappedRightButton userInfo:@{@"indexPath" :self .indexPath}]; } @end
总结
使用这种基于Responder Chain的方式来传递事件,在复杂UI层级的页面中,可以避免无谓的delegate声明。
事件处理的逻辑得到归拢,在这个方法里面下断点就能够管理所有的事件处理。
参考文章 Using Responders and the Responder Chain to Handle Events 一种基于ResponderChain的对象交互方式 responderChainDemo