Notification的工作机制
1.对应用程序中其他地方发生的事件(比如增加一条数据库记录)感兴趣的对象,会向通告中心(Notification Center,NSNotificationCenter类的实例)注册,从而成为该事件的监听者。在注册过程中,监听者需要指定方法供通告中心在事件发生时调用。
2.给数据库添加记录(或者类似事件)的对象给通告中心发一个通告(NSnotification的实例)。该通告对象包括识别通告的标志、发布通告的对象ID和可选的附加信息字典。
3.通告中心发送消息到每个已注册的监听者,调用监听者指定的方法会将通告传给这些监听者。
NSNotification对象
NSNotification包含三个属性:
name一个notification对象的name,由我们自己定义
objectpost该notification的object,也就是sender
userInfo伴随notification传递过去的数据,是一个NSDictionary
NSNotificationCenter中的方法
Getting the Notification Center
+ defaultCenter该方法用于获取当前程序中的center object
Managing Notification Observers
– addObserverForName:object:queue:usingBlock:
对快代码的操作,详见:http://blog.sina.com.cn/s/blog_63ced45101016cih.html
– addObserver:selector:name:object:
notificationObserver 一个对象,作为observer,不能为空。
notificationSelectorobserver接收到notificaiton后会调用的方法,该方法只能有一个参数,类似:- (void) mytest:(NSNotification*) notification
notificationNamenotification的name,center只会像该observer发送指定name的notification
notificationSender一个对象,作为sender,也就是说指定了只有改sender发送了name为上一个参数指定的notification,center才会向observer发送notification
– removeObserver:
notificationObserver 从center中删除observer,add方法调用后,要在合适的地方调用remove方法删除observer,否则如果多系调用add后,一个post会接收到多次notification
– removeObserver:name:object:
该方法与–addObserver:selector:name:object:结合起来使用
Posting Notifications
– postNotification:
NSNotification
– postNotificationName:object:
notificationNamenotification name
notificationSender发送notification的object
– postNotificationName:object:userInfo:
notificationNamenotification name
notificationSender发送notification的object
userInfo伴随着notification发送过去的数据,是一个NSDictionary
Android中的四大组件之一Broadcast用来在手机上发送广播,无论是同一个程序中或是在其他的应用中,只要你注册接收该条广播,那么当广播发送之后,你就会收到,这种机制可以降低程序中间的耦合性,可以通过发送广播调用其他类中的东西。
在IOS中也有类似Broadcast的机制,其中包括NSNotificationCenter和NSDistributedNotificationCenter机制。
NSNotificationCenter。在IOS运行的每一个程序都有一个default center,不要自己创建。通过方法[NSNotificationCenter defaultCenter]可以获取center对象,center实际上是notification的dispatch table(调度表),center控制着程序中所有notification的添加、删除以及post后的响应。可以说NSNotificationCenter 是专门供一个程序中的不同类间的消息通信而设置的。
NSDistributedNotificationCenter用于在不同程序之间post和receive notification,这个跟Android的Broadcast比较相似。
相关推荐
iOS基础——通知代理之NSNotificationCenter、Delegate
iOS 通知 转发
iOS基础——通知代理之NSNotificationCenter、Delegate
SFObservers, NSNotificationCenter和KVO自动删除观察者 在任何项目中,我不再使用 SFObservers,但仍然会接受请求的请求。命令行目SFObservers是在NSNotificationCenter和KVO中为观察者 Pattern 添加自动删除的类别...
NULL 博文链接:https://o0o0o0o.iteye.com/blog/645602
IOS中经常会使用到NSNotification和delegate来进行一些类之间的消息传递,这篇文章主要介绍了iOS NSNotificationCenter使用小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
详细说明了NSNotificationCenter的使用,包括各个参数的使用
通知中心(NSNotificationCenter)采用单例的模式,整个系统只有一个通知中心,通过如下代码获取: //获取通知中心 [NSNotificationCenter defaultCenter]; 注册通知监听器方法: //observer为监听器 //aSelector...
OC-NSNotificationCenter.异步线程下载图片zip
iOS开发基础-NSNotificationCenter(通知中心)通知发布者和监听者示例,代码DEMO附带详细注释。有错误的地方欢迎提出。
IZEvent IZEvent是NSNotificationCenter的纯Swift替代方案。 它努力做到简单,安全和简单。 Pro的NSNotificationCenter上: 内存安全:不会发生内存泄漏,也不需要在deinit中删除观察者线程安全:默认情况下将事件...
FXNotifications, 一个NSNotificationCenter的替代 API 命令行目FXNotifications是 NSNotificationCenter的一种类别,提供了基于基于的API,更简单易用,并避免了官方API的不同内存漏洞。有关更多详细信息,请参见...
NULL 博文链接:https://andydufresneone.iteye.com/blog/1028462
一个简单的NSNotificationCenter的使用例子Demo,适合初学者。另外一个简单的UI效果是在tab bar的tab上面加上数字badge。 小编注:感谢开发者@Promise松 分享代码于本站。 Code4App编译测试,适用环境:Xcode ...
A 页有N+1UIView,B页执行一个操作,A的N+1Uiview要修改,可以通过Delelgate实现,蛋有的时候无法实现时,选用消息模式
ios demo,NSNotificationCenter,app进入后台时的调用和就、进入前台时的调用。
1.提供添加观察者、发送通知接口 2.封装类中已实现remove观察者 3.通过block回传发送者发送的信息
NULL 博文链接:https://justcoding.iteye.com/blog/1476165