IOS中通知中心的使用(传值)

通知中心可以实现从后一个界面向前一个界面传值的功能。(这里认为是从b界面向a界面传值)

第三个界面的代码

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@(downLoadProgress),@"progress",progressStr,@"progressStr",@(totalBytesExpectedToWrite),@"totalProgress", nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"ViewController" object:@"zhangsan" userInfo:dic];

从b界面发送一条通知,其中Name为第一个界面的名称,object为要传递的值,如果使用单值传递,只需将所要传的值赋给object即可,如果要使用多值传递,可以将要传递的值放入一个字典,将值传递过去,object除了可以传递值之外,还可以作为标识符。

第一个界面的代码

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

[center addObserver:self selector:@selector(receiveNotificiation:) name:@"ViewController" object:@"zhangsan"];

//通知中心的回调方法

- (void)receiveNotificiation:(NSNotification*)sender{

dispatch_async(dispatch_get_main_queue(), ^{

double progress = [[sender.userInfo objectForKey:@"progress"] doubleValue];

self.downLoadProgressView.progress = progress;

self.currentProgress_label.text = [sender.userInfo objectForKey:@"progressStr"];

});

}

//将通知中心移除

- (void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ViewController" object:@"zhangsan"];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容