一.通知原理
通知是iOS开法中的几种常见传值方法中的一种,通知适合一对多的广播式传值.
二.通知需注意
1.通知玩需要关闭通知,以免内存泄露;
2.通知一旦关闭将不会再行调用
三.通知的一点秘密
1.通知重新调用比较适合在程序再次启动,但是使用不同的页面跳转可以使通知再次被调用,具体的可以去试一试.
2.能使用其他几种传值尽量不要使用通知
四,通知使用的核心代码
发送一个字典传值
1.建值
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:xxx,@"xxxkey",nil];
2.新建noticefication
NSNotification *noticefication = [NSNotificationnotificationWithName:@"notificationName"object:niluserInfo:dic];
3.send the noticefication
[[NSNotificationCenterdefaultCenter]postNotification:noticefication];
发送一个值作为指令
[[NSNotificationCenter defaultCenter] postNotificationName:@"editservice" object:nil userInfo:@{@"edit" : @(1)}];
接收即是监听
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(hereiscrossedData:) name:@"sendTownSave"object:nil];
- (void)hereiscrossedData:(NSNotification*)info{
NSLog(@"%@",info.userInfo);
}
5.完事,记得关闭通知
[[NSNotificationCenter defaultCenter] removeObserver:self];