开发中经常有子视图需要把事件传递给父视图,除了代理和block还有更好的选择,这里记录一下tsaievan的Router.
@interface UIResponder (Router)
- (void)routerWithEventName:(NSString *)eventName userInfo:(NSDictionary *)userInfo;
@end
@implementation UIResponder (Router)
//把事件向上层响应者传递
- (void)routerWithEventName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
if (self.nextResponder) {
[[self nextResponder] routerWithEventName:eventName userInfo:userInfo];
}
}
@end
- (void)routerWithEventName:(NSString *)eventName userInfo:(NSDictionary *)userInfo {
if ([eventName isEqualToString:YourCustomNameEvent]) {
NSString * name = userInfo[YourCustomKey];
NSLog(name);
}
}
- (void)buttonClickAction:(UIButton *)sender {
[sender routerWithEventName:YourCustomNameEvent userInfo:@{
YourCustomKey:[self userName],
}];
}