目的:
通过通知传递数据.
步骤:
1.注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:TESTNOTIFICATION object:nil];
2:注册通知实现的方法:
- (void)tongzhi:(NSNotification *)text{
NSDictionary *dic = text.userInfo;
NSLog(@"%@",[dic objectForKey:@"key1"]);
NSLog(@"-----接收到通知------");
}
3: 把数据通过通知传递:
NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil];
// 方法1:
//创建通知
NSNotification *notification =[NSNotification notificationWithName:TESTNOTIFICATION object:nil userInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
//方法2:
[[NSNotificationCenter defaultCenter] postNotificationName:TESTNOTIFICATION object:self userInfo:dict];
4: 移除通知:
[[NSNotificationCenter defaultCenter] removeObserver:self name:TESTNOTIFICATION object:nil];