使用NSNotificationCenter向另一个类发出通知 [英] Giving notification to another class with NSNotificationCenter

查看:89
本文介绍了使用NSNotificationCenter向另一个类发出通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的目标是使用NSNotificationCenter向另一个类传递通知,我还想将带有通知的object传递给另一个class,我应该怎么做?

So my goal is to deliver a notification to another class with using NSNotificationCenter, I also want to pass object with the notification to the other class, how should I do this?

推荐答案

您必须先注册一个通知名称

You must first register a notification name

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":"

然后发布带有参数字典的通知

And then post a notification with a dictionary of parameters

[[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 

方法将是

- (void)startLocating:(NSNotification *)notification {

    NSDictionary *dict = [notification userInfo];
}

这篇关于使用NSNotificationCenter向另一个类发出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆