概述
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實(shí)現(xiàn)對(duì)象交互
我們可以借用responder chain實(shí)現(xiàn)了一個(gè)自己的事件傳遞鏈。
//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、創(chuàng)建簽名對(duì)象
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:@"方法沒(méi)有" reason:info userInfo:nil];
return nil;
}
//3忧换、提鸟、創(chuàng)建NSInvocation對(duì)象
NSInvocation*invocation = [NSInvocation invocationWithMethodSignature:signature];
//4哗脖、保存方法所屬的對(duì)象
invocation.target = self;
invocation.selector = aSelector;
return invocation;
}
@end
在需要響應(yīng)事件的類中重載routerEventWithName::
方法
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
[self.eventProxy handleEvent:eventName userInfo:userInfo];
}
使用EventProxy
類來(lái)專門處理對(duì)應(yīng)的事件
//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 左邊按鈕被點(diǎn)擊啦介杆!",indexPath.section, indexPath.row);
}
- (void)cellMiddleButtonClick:(NSDictionary *)userInfo {
NSIndexPath *indexPath = userInfo[@"indexPath"];
NSLog(@"indexPath:section:%ld, row:%ld 中間按鈕被點(diǎn)擊啦皆看!",indexPath.section, indexPath.row);
}
- (void)cellRightButtonClick:(NSDictionary *)userInfo {
NSIndexPath *indexPath = userInfo[@"indexPath"];
NSLog(@"indexPath:section:%ld, row:%ld 右邊按鈕被點(diǎn)擊啦家破!",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
的事件中颜说,調(diào)用routerEventWithName:userInfo:
方法,就會(huì)調(diào)用到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
總結(jié)
- 使用這種基于Responder Chain的方式來(lái)傳遞事件门粪,在復(fù)雜UI層級(jí)的頁(yè)面中,可以避免無(wú)謂的delegate聲明烹困。
- 事件處理的邏輯得到歸攏玄妈,在這個(gè)方法里面下斷點(diǎn)就能夠管理所有的事件處理。
參考文章
Using Responders and the Responder Chain to Handle Events
一種基于ResponderChain的對(duì)象交互方式
responderChainDemo