iOS开发iOSiOS基础小知识

【iOS】传值方式——通知

2017-11-17  本文已影响13人  清都

一般正向传值使用属性,而反向传值使用协议或Block即可,但是我们有时候也会遇到需要隔页面传值的情况,比如一连回退超过一个页面,此时就需要使用通知进行传值了。
使用通知需要按照步骤进行,先在目标文件注册通知,再发送通知,不然目标文件无法接收通知信息。代码在程序运行时的顺序很重要,必须是先注册后发送。

注册通知


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doNotification:) name:@"aNotification" object:nil];

发送通知


NSDictionary * dic = @{@"price":@(1024.66),@"name":@"aName"};
[[NSNotificationCenter defaultCenter] postNotificationName:@"aNotification" object:nil userInfo:dic];

注册通知处的执行方法


-(void)doNotification:(NSNotification *)notifocation{

//接收通知并执行对应方法

}

移除通知


[[NSNotificationCenter defaultCenter]removeObserver:self name:@"aNotification" object:nil];
上一篇 下一篇

猜你喜欢

热点阅读