iOS关于通知的那些事

一.通知原理

通知是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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值