iphone开发 通知NSNotification的运用

来源:互联网 发布:一次性内裤 知乎 编辑:程序博客网 时间:2024/06/03 05:16

有时候触发某个时间的时候想通知别的类进行相应的处理,这种情况可解决的方式很多。

比如继承、自定义delegate、通知等。

在此我说明下通知的运用。


事件A的触发,想让B做出相应的处理。

1、在B中注册通知:

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

2、在B中实现接收到通知的回调函数

- (void)ButtonClick:(NSNotification *)notification{}

3、在A中事件触发时候实现post通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"XXX"                                                                     object:self                                                                  userInfo:nil];

在userInfo参数中需要的话也可以传递一些信息,放在NSDictionary中传递过去,然后在B的ButtonClick函数里通过[notification userInfo]获取


原创粉丝点击