简单介绍NSNotificationCenter
来源:互联网 发布:如何申请多个淘宝小号 编辑:程序博客网 时间:2024/05/19 06:51
ios开发中有时会用到NSNotificationCenter,其实NSNotificationCenter的原理是一个观察者模式,包括了观察者的注册、通知及删除等。
获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter],通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。
很多消息都会通过通知中心分发,比如你想在键盘收起时做一些事情,就可以写一个方法,比如叫keyboardDidHide:,然后只需要加上如下代码:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
这样,就可以在键盘收起的时候调用当前类的keyboardDidHide:方法了。类似UIKeyboardDidHideNotification这样的消息名还有很多,如UIKeyboardDidShowNotification,UIWindowDidResignKeyNotification等等。
我们如果想定制自己的消息名也是很简单的
NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter];
[notifyCenter addObserver:self selector:@selector(等到通知后调用的方法) name:你的消息名,必须是NSString类型 object:nil];
而发送这个消息其实也只需几行代码
NSNotificationCenter * notifyCenter = [NSNotificationCenter defaultCenter];
NSNotification *nnf = [NSNotification notificationWithName:你的消息名,必须是NSString类型 object:新建一个NSNotification需要的的对象];
[notifyCenter postNotification:nnf];
- 简单介绍下NSNotificationCenter
- 简单介绍下NSNotificationCenter
- 简单介绍NSNotificationCenter
- NSNotificationCenter介绍
- NSNotificationCenter的简单应用
- NSNotificationCenter的简单应用
- NSNotificationCenter 的简单使用
- NSNotificationCenter简单使用
- NSNotificationCenter消息通信机制介绍
- NSNotificationCenter的简单使用总结
- ios NSNotificationCenter 的简单使用
- iOS NSNotificationCenter的简单使用
- 一个简单的NSNotificationCenter使用
- NSNotificationCenter 用法 一个简单的使用 NSNotificationCenter 的代码例子
- NSNotificationCenter
- NSNotificationCenter
- NSNotificationCenter
- NSnotificationcenter
- PHP SPL 介绍
- 风的世界里有海的七滴泪水
- /example/xdom/note.xml是指: http://www.w3school.com.cn/example/xdom/note.xml
- Eclipse对printf()不能输出到控制台的解决方法
- 如何才能做到网站高并发访问?
- 简单介绍NSNotificationCenter
- 如何恰当地进行网站改版?
- 序列化和反序列化有几种方式?分别是什么?
- MFC非模态对话框使用详解
- 设计模式二--工厂方法模式
- Java对象的地址
- 折半查找之我见
- 没有任何IE的工具栏,地址栏等
- 出行所带物品列表(供以后参考)