Notification和Delegates
来源:互联网 发布:开源人工智能项目 编辑:程序博客网 时间:2024/06/07 22:37
看这里
Notification
通知(Notifications)提供了一种从任何地方向任何地方广播消息的方法。Objective-C中的NSNotification类就能实现这样的功能。严格地说,它不是语言的一部分,二是Foundation框架的一部分。但是,你在使用Objective-C时,几乎不可能不使用Foundation。NSNotification的实例通过一个NSNotificationCenter进行广播。
通知包含一个name、一个object和一个元数据字典。object和元数据是可选的,name是必需的。object可以通过向通知中心注册来接受某种通知,这些通知通过name、object或者name和object一起来进行过滤匹配。另外,可以传递一个selector,这样当通知匹配时可以调用。
一个示例代码如下:
//从一个对象注册为观察者[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(thingHappened:) name:MyThingHappened object:_myObject];//从另一个对象发送通知[[NSNotificationCenter defaultCenter] postNotificationName:MyThingHappend object:self userInfo:nil];在这个例子中,一个对象将自己注册为观察者,来接收MyThingHappened通知,并且限制接受对象为_myObject来的该通知。然后,另外一个对象提交通知,将自己作为object,没有元数据(userInfo)。这种情况下,如果提交该通知的的对象是来自注册过的对象上下文中的_myObject,那么该通知将触发thingHappened:调用。
Delegates
delegate定义了一个接口,通过该接口,两个对象之间可以进行交互。在Objective-C中,这常常通过使用@protocol语法的形式化协议来实现。在delegate场景中,一个对象是delegate,另一个对象是delegator。delegator会引用它的delegate,这通过delegator调用低音在delegate协议中的某个方法来发送消息实现。一个示例是在用户接口中的按钮,可以有一个delegate处理按钮点击事件通知。
- Notification和Delegates
- Delegates 和Events的应用
- Delegates 和 Events 在unity中的使用
- Events and Delegates(事件和委托)
- 注意在代码中如何和何处使用delegates
- 8.10 Delegates
- 22. Delegates
- Java delegates
- Delegates internals
- 默认的Notification和自定义的Notification
- Notification和FreeNotification
- Toast和Notification
- 创建和触发Notification
- NotificationManager和Notification
- UserDefault和Notification
- 练习Toast和Notification
- android11_NotificationManager和Notification
- KVO和Notification
- iphone中的UITouch
- 播放amr格式的音频
- 数据校验
- Linux 下 去掉终端显示的当前目录的绝对路径
- 动态给剪辑对象增加属性
- Notification和Delegates
- 双向链表的C实现
- PL/SQL 导出数据为Excel ,同时将Excel数据导入
- 不懂技术,怎样制作手机电子书?
- VC操作剪切板
- 强制性认证覆盖百余种工业产品
- u-boot链接脚本解析
- NodeJS学习笔记
- 任意美化你的文件域 <input type="file" />