消息中心的使用
来源:互联网 发布:淘宝图片多少厘米 编辑:程序博客网 时间:2024/04/29 19:23
消息中心在IOS中经常被用到。
当一个某个条件被满足的时候,就可以使用消息中心把消息抛出。
如果满足某些条件时进行简单的通知抛出,则使用- (void)postNotification:(NSNotification *)notification
如果满足某些条件之后进行抛出并希望附带当前环境下的值,则使用- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo,把值塞进useInfo中进行传递。
在相应地方进行通知的捕获和处理,使用- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
一般来说,在捕获的地方对通知都使用统一的处理(SEL)notificationSelector,其代码中使用switch语句来分别处理不同的通知,使代码整齐。
最后,需要释放掉相应的通知,使用[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
抛出通知:
[[NSNotificationCenter defaultCenter]postNotificationName:@"test1" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:Time] forKey:@"TEST"]];
注册通知并做响应:[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didReceiveNotification:) name:@"test1" object:nil];
处理方法
- (void)didReceiveNotification:(NSNotification*)aNoti
{
NSString* name = aNoti.name;
if([name isEqualToString:@"test1"]){
NSLog(@"test:%@",[[aNoti userInfo] objectForKey:@"TEST"]);
[[NetworkClock sharedNetworkClock] finishAssociations];//释放test1消息通知
}
}
- 消息中心的使用
- 使用 Netty 搭建消息中心
- objective 消息中心的处理
- 通知中心的使用
- ios-消息中心 NSNotificationCenter 的介绍
- 对消息中心CCNotificationCenter的封装
- 消息调度中心的学习资料汇总
- 基于Redis的消息中心缓存搭建
- Rabbitmq消息中心_消息中心总体方案
- RabbitMqx消息中心_消息中心一致性
- 通知中心NSNotificationCenter的使用
- 简化通知中心的使用
- iOS 通知中心的使用
- 通知中心的简单使用
- 简化通知中心的使用
- iOS通知中心的使用
- 消息中心上线了
- 消息中心 NSNotificationCenter
- 云计算架构
- linux进程调度方法(SCHED_OTHER,SCHED_FIFO,SCHED_RR)
- 文件对象,文件系统设备,卷设备之间的关系说明
- C++ COM 调用这个DLL
- JAVAMAIL发送邮件实例实现过程
- 消息中心的使用
- 题目1060:完数VS盈数
- 以太网接口配置
- Android crash之NoClassDefFoundError
- SMB协议分析
- 数据库事务ACID特性
- ORA-12154: TNS: 无法解析指定的连接标识符 解决方法
- resultSet2Bean
- Http Post发送json序列请求(json序列化和反序列化)