概述
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實現了一個自己的事件傳遞鏈。
//UIResponder的分類
//.h文件
#import <UIKit/UIKit.h>
@interface UIResponder (Router)
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo;
@end
//.m文件
#import "UIResponder+Router.h"
@implementation UIResponder (Router)
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
[[self nextResponder] routerEventWithName:eventName userInfo:userInfo];
}
@end
//NSObject
//.h文件
#import <Foundation/Foundation.h>
@interface NSObject (Invocation)
- (NSInvocation *)createInvocationWithSelector:(SEL)aSelector;
@end
//.m文件
#import "NSObject+Invocation.h"
@implementation NSObject (Invocation)
- (NSInvocation *)createInvocationWithSelector:(SEL)aSelector {
//1、創建簽名對象
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:aSelector];
//2、判斷傳入的方法是否存在
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;
}
//3、、創建NSInvocation對象
NSInvocation*invocation = [NSInvocation invocationWithMethodSignature:signature];
//4、保存方法所屬的對象
invocation.target = self;
invocation.selector = aSelector;
return invocation;
}
@end
在需要響應事件的類中重載routerEventWithName::
方法
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
[self.eventProxy handleEvent:eventName userInfo:userInfo];
}
使用EventProxy
類來專門處理對應的事件
//EventProxy.h
#import <Foundation/Foundation.h>
@interface EventProxy : NSObject
- (void)handleEvent:(NSString *)eventName userInfo:(NSDictionary *)userInfo;
@end
//EventProxy.m
#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
類中的方法。
@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