NSNotificationCenter 的简单使用
来源:互联网 发布:淘宝上的开团提醒 编辑:程序博客网 时间:2024/05/16 15:20
1.先成为观察者(B)
B.h
- (void)becomeObserve;
B.m
- (void)becomeObserve { // 在消息通知中心订阅一条名为“dataChange”的消息,当消息发出时,会通知 self调用selector中的方法 object如果设为nil,表示会收到所有这个名字的消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updataUI) name:@"dataChange" object:nil];}
2.发送通知(A)
A.m
// 当数据发生变化时,需要将此消息发布出去(如果对象关心此消息就需要提前订阅) // 去消息通知中心发送广播 [b <span style="font-family: Arial, Helvetica, sans-serif;">becomeObserve];<span style="white-space:pre"></span>// </span><span style="font-family: Arial, Helvetica, sans-serif;">b成为观察者</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span></span> //第一个参数 消息的名称 第二个参数 发送消息的对象 //[[NSNotificationCenter defaultCenter] postNotificationName:@"dataChange" object:nil]; // 发布消息的同时传递数据,需要将数据封装成个字典 NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Kermit",@"name", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"dataChange" object:nil userInfo:dict];
3.退订消息(不再成为观察者)
B.m
- (void)dealloc { // 退订消息(不再成为观察者)当“dataChange”消息来到时不再关心,处理,object表示发出消息的对象 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"dataChange" object:nil];}
0 0
- NSNotificationCenter 的简单使用
- NSNotificationCenter的简单使用总结
- ios NSNotificationCenter 的简单使用
- iOS NSNotificationCenter的简单使用
- 一个简单的NSNotificationCenter使用
- NSNotificationCenter 用法 一个简单的使用 NSNotificationCenter 的代码例子
- iOS中NSNotificationCenter的简单使用
- NSNotificationCenter简单使用
- NSNotificationCenter的简单应用
- NSNotificationCenter的简单应用
- iOS监听模式系列之NSNotificationCenter的简单使用
- NSNotificationCenter 的使用
- NSNotificationCenter 的使用
- NSNotificationCenter 的使用
- NSNotificationCenter 的使用
- NSNotificationCenter 的使用
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- 集合栈-程序员面试金典
- HTML5 Web Worker的使用
- Intergraph CADWorx 2016下载 v16.0
- Linux内核基础--事件通知链(notifier chain)
- 冒泡排序java版
- NSNotificationCenter 的简单使用
- Block总结
- Kd-Tree算法原理和开源实现代码
- 机器学习 scikit-learn 图谱
- 欢迎使用CSDN-markdown编辑器
- sphinx教程2__安装、配置和使用
- 万年历api接口调用文档
- Intellij java: 未结束的字符串文字
- Qt-直接将xxx.ui文件转化为ui_xxx.h文件