一.通知的使用方法

  1.发出通知

    例如:[[NSNotificationCenter defaultCenter]postNotificationName:@"backToFirstPage" object:@"123321"];

    通知的名字叫做backToFirstPage,而123321是一个参数,类型为ID类型,一般用于传值的

  2.接受通知

    例如:[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiceNot:) name:@"backToFirstPage" object:nil];

    观察者就是self

    接收通知的名字:backToFirstPage

    接收到后调用的函数- (void)receiceNot:(NSNotification)nt{};nt.objet 就是发出通知传过来的参数@"123321"

  3.移除通知

    例如:[[NSNotificationCenter defaultCenter]removeObserver:self name:@"backToFirstPage" object:nil];

二:常用场景

   通知属于一对多的,可以用于发出通知可以被多个类同时接收,这个于代理不相同,代理是一对一的。

三:注意事项

  1.发出通知以后,当另一个类接收到了通知,如果不需要持续监听的情况下,最好是立即移除监听。因为这个类释放了的时候,还没有移除监听的话,程序会出现异常崩溃。

  2.接收通知后,只要这个类没有被释放,就会持续的监听这个通知。

  3.移除通知以后,该类将不再接收通知(只是不接收名为“backToFirstPage”)的通知

四:程序中常用的一些监听方法

  1.键盘监听

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
    //添加切入后台切回前台监听didEnterBackground:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:nil];
    //监听后台返回前台的时候调用willEnterForeground:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification object:nil];

  //监听后台返回前台的时候调用willEnterForeground:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

  //如果程序在前台运行, 也就是播放讲解。此时来了个电话或者闹铃, 会触发

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:)  name:AVAudioSessionInterruptionNotification object:nil];

观察者模式:关于通知的使用(NSNotificationCenter)的更多相关文章

  1. OC 观察者模式(通知中心,KVO)

    OC 观察者模式(通知中心,KVO) 什么是观察者模式??? A对B的变化感兴趣,就注册为B的观察者,当B发生变化时通知A,告知B发生了变化.这就是观察者模式. 观察者模式定义了一种一对多的依赖关系, ...

  2. Swift - 使用NSNotificationCenter发送通知,接收通知

    转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...

  3. 通知 - NSNotificationCenter

    1.每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信: 2.任何一个对象都可以向通知中心发布通知(NSNotification), 描 ...

  4. NSNotificationCenter通知中心

    概述 NSNotificationCenter通知中心,通常用于一对一或者一对多的消息传递,即当一个地方改变时,要求改变其他的一些地方,例如当网络请求回来了新的数据,需要刷新本地信息和本地内存里面的界 ...

  5. iOS中通知中心NSNotificationCenter应用总结

    通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制.把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象.这句话其实已经很明显的告诉我们要如何使用通知了.第 ...

  6. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  7. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  8. IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变

    监听文本框的文字改变 * 一个文本输入框的文字发生改变时,文本输入框会发出一个UITextFieldTextDidChangeNotification通知 * 因此通过监听通知来监听文本输入框的文字改 ...

  9. iOS通知的使用

    注册:[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:self]; ...

  10. 你真的了解NSNotificationCenter吗?

    一:首先查看一下关于NSNotificationCenter的定义 @interface NSNotificationCenter : NSObject { @package void * __str ...

随机推荐

  1. Hide/Show running Console

    http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-applica ...

  2. How to: Modify a Project System So That Projects Load in Multiple Versions of Visual Studio

    http://msdn.microsoft.com/en-us/library/hh266706(v=VS.110).aspx

  3. web.xml 中的listener、 filter、servlet 加载顺序

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  4. Heritrix源码分析(十一) Heritrix中的URL--CandidateURI和CrawlURI以及如何增加自己的属性(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/649889 本博客已迁移到本人独立博客: http://www.yun5u.com/ ...

  5. 《Write Optimized B-Trees》读书报告

    论文原作者:Goetz Graefe, Microsoft.我读完这篇论文后颇有收获,所以写了一篇论文报告,旨在更精炼准确地阐述论文核心思想. 摘要:论文提出了一种方法,这种方法可以优化B树索引写性能 ...

  6. APP打包上线应注意的问题!

    咱们只谈技术不谈业务!关系到怎么推广怎么让软件发扬光大,其实归根结底这都和公司的现状和产品经理有直接的联系,与我们程序员关系不太密切.   我总结的上线项目和我做的项目以及公司外包过来的项目升级再次发 ...

  7. ios 内存管理 心得

    - alloc, copy, retain会把引用计数+1 - release会把引用计数-1 - 局部变量如果初始化时不是autorelease的,要及时调用release释放,并且赋值为nil否则 ...

  8. Supervisor行为分析和实践

    1.简介     Erlang要编写高容错性.稳定性的系统,supervisor就是用来解决这一问题的核心思想.通过建立一颗监控树,来组织进程之间的关系,通过确定重启策略.子进程说明书等参数信息来确定 ...

  9. Brush、Color、String相互转换

    using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  10. Nginx - Windows下Nginx基本安装和配置

    Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定.性能更好,而且配置简单,资源占用较低. 1. 安装 Nginx ...