NSInvocation 与策略模式
来源:互联网 发布:spark 提交python脚本 编辑:程序博客网 时间:2024/06/18 13:26
- (NSDictionary <NSString *, NSInvocation *> *)eventStrategy{ if (_eventStrategy == nil) { _eventStrategy = @{ @"kBLGoodsDetailTicketEvent":[self createInvocationWithSelector:@selector(ticketEvent:)], @"kBLGoodsDetailPromotionEvent":[self createInvocationWithSelector:@selector(myLog:)], }; } return _eventStrategy;}- (void)ticketEvent:(NSDictionary *)userInfo{ NSLog(@"%@ticketEvent",userInfo);}- (NSDictionary *)myLog:(NSDictionary *)userInfo{ NSLog(@"%@ticketEvent",userInfo); return userInfo;}#pragma mark - event response- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{ NSLog(@"%@",eventName); NSInvocation * invocatin = self.eventStrategy[@"kBLGoodsDetailTicketEvent"]; [invocatin setArgument:&userInfo atIndex:2]; [invocatin invoke]; // 如果需要让事件继续往上传递,则调用下面的语句 // [super routerEventWithName:eventName userInfo:userInfo];}- (NSInvocation *)createInvocationWithSelector:(SEL)myMethod2 { NSMethodSignature * sig = [[self class] instanceMethodSignatureForSelector:myMethod2];//有参数的时候 NSInvocation * invocatin = [NSInvocation invocationWithMethodSignature:sig]; [invocatin setTarget:self]; [invocatin setSelector:myMethod2]; return invocatin;}
直接贴代码
当有点击事件的时候
会发给routerEventWithName 方法
按照多个事件回传的时候,
可能要根据点击事件处理不同的方法
需要写大量的if else
避免 这种方式 就是策略模式
一个字典 保存所有的方法
回传根据方法名称 来决定 调用哪个方法
NSInvocation 做了中转处理
关键代码是[invocatin setArgument:&userInfo atIndex:2];
就是把userinfo 赋值给 第二个参数
如果要增加参数 就再后面付给 第3 个 第 4 个 就完成了
‘[invocatin setArgument:&userInfo atIndex:3];`
阅读全文
0 0
- NSInvocation 与策略模式
- NSInvocation与异常处理
- Spring与策略模式
- JavaScript与策略模式
- Spring与策略模式
- 策略模式与机制
- 策略模式与合成模式
- 策略模式与状态模式
- 策略模式与桥梁模式
- 策略模式与外观模式
- 代理模式与策略模式
- 命令模式与策略模式
- 策略模式与状态模式
- 状态模式与策略模式
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- Android中The SDK platform-tools version ((23)) is too old to check APIs compiled with API 23
- Centos rpm安装 、 卸载JDK
- ACE教程全集
- Java虚拟机垃圾回收(二) 垃圾回收算法 标记-清除算法 复制算法 标记-整理算法 分代收集算法 火车算法
- Solr使用入门指南
- NSInvocation 与策略模式
- 统计学习方法 笔记与总结 决策树
- Python爬虫利器三之Xpath语法与lxml库的用法
- FullCalendar
- linux创建用户组 以及查询chat相关用户与用户组信息
- ETCD集群搭建
- 数字河(模拟)
- “城市大脑”治城一年 杭州“变”了
- BZOJ 2321 星器 能量守恒定律