一.通知的使用方法

  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. 关于UNION ALL与 UNION 用法和区别

    (转自:http://www.cnblogs.com/EricaMIN1987_IT/archive/2011/01/20/1940188.html) UNION指令的目的是将两个SQL语句的结果合并 ...

  2. 最大熵模型 Maximum Entropy Model

    熵的概念在统计学习与机器学习中真是很重要,熵的介绍在这里:信息熵 Information Theory .今天的主题是最大熵模型(Maximum Entropy Model,以下简称MaxEnt),M ...

  3. Java自动装箱拆箱

    一.装箱.拆箱定义 如果一个int型量被传递到需要一个Integer对象的地方,那么,编译器将在幕后插入一个对Integer构造方法的调用,这就叫做自动装箱.而如果一个Integer对象被放到需要in ...

  4. 操作系统——CPU、计算机的构成

    CPU主要由什么构成? CPU包括运算逻辑部件.寄存器部件和控制部件等. 逻辑部件:可以执行点或浮点算术远算操作.移位操作以及逻辑操作,也可以执行地址运算和转换. 寄存器部件:存储程序.数据和各种信号 ...

  5. JVM——判断对象的死活

    一.引用计数法 给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1,当引用失效时,计数器值就减1,任何时刻计数器为0的对象就是不可能再被使用的. 但是它很难解决对象之间相互循环引用的问 ...

  6. http tcp联系区别

    术语TCP/IP代表传输控制协议/网际协议,指的是一系列协议.“IP”代表网际协议,TCP和UDP使用该协议从一个网络传送数据包到另一个网络.把IP想像成一种高速公路,它允许其它协议在上面行驶并找到到 ...

  7. geotools导出shapefile出错: java.io.IOException: Current fid index is null, next must be called before write()

    geotools导出shapefile出错: java.io.IOException: Current fid index is null, next must be called before wr ...

  8. [转] C# 键盘中的按键对应的KeyValue

    原文C#KeyValue对应键盘 首先先看一下什么情况下需要对按键进行识别: KeyPress事件响应函数中,有KeyPressEventArgs, 对应于e.KeyChar; KeyDown事件响应 ...

  9. [Papers]MHD, $\p_3\pi$, Lebesgue space [Cao-Wu, JDE, 2010]

    $$\bex \p_3\pi\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{12}{7},\quad \frac{12}{7} ...

  10. HDU5828 Rikka with Sequence 线段树

    分析:这个题和bc round 73应该是差不多的题,当时是zimpha巨出的,那个是取phi,这个是开根 吐槽:赛场上写的时候直接维护数值相同的区间,然后1A,结果赛后糖教一组数据给hack了,仰慕 ...