一.通知的使用方法

  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. 13.Object-C--浅谈Foundation框架常用的结构体

    ------- android培训.iOS培训.期待与您交流! ---------- 昨天学习了Foundation框架中常用的结构体,下面我简单的总结一下,如果错误麻烦请留言指正,谢谢! Found ...

  2. 移植yaffs文件系统

    需要下载yaffs2-d43e901.tar.gz,busybox-1.13.0.tar.bz2 使用的交叉编译器是4.33 1.修改配置编译busybox 修改Makefile CROSS_COMP ...

  3. 20160128.CCPP体系详解(0007天)

    以下内容有所摘取,进行了某些整理和补充 论浮点数的存储原理:float浮点数与double浮点数的二进制存储原理–>阶码 浮点数转二进制 1.整数int类型和浮点数float类型都是占用4个字节 ...

  4. Aviary 滤镜 教程 照片编辑器

    Aviary是一个国外的非常强大的照片编辑器,各种功能,但是是以静态库的形式存在的,不开源,但是很好用. 1.到官网上面下载sdk https://github.com/AviaryInc/Mobil ...

  5. jboss集成eclipse

    eclipse Kepler + Jboss7.1 参考引用文档: http://www.tekdigest.com/how-to-install-jboss-tools-in-eclipse.htm ...

  6. Word2003使用VBA教程

    [正文] 注:本文中所有vba代码都是储存在doc中,而非normal.dot 1.打开一个.doc文档 2.按ALT+F11 3.左侧 Project-插入-模块 4.输入自己的代码,一定要是函数的 ...

  7. Symfony2学习笔记之数据校验

    校验在web应用程序中是一个常见的任务.数据输入到表单需要被校验.数据在被写入数据库之前或者传入一个webservice时也需要被校验. Symfony2 配备了一个Validator 组件,它让校验 ...

  8. C#-gdi绘图,双缓冲绘图,Paint事件的触发

    一. 画面闪烁问题与双缓冲技术 1.1 导致画面闪烁的关键原因分析: 1  绘制窗口由于大小位置状态改变进行重绘操作时 绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面 ...

  9. MSSQL 查询分组前N条记录

    sql语句中,查询分组中前n条记录的sql语句如下 第一种方法 select * from consultingfeebill awhere n>(select count(*) from co ...

  10. asp.net下ajax.ajaxMethod使用方法(转)

    使用AjaxMethod可以在客户端异步调用服务端方法,简单地说就是在JS里调用后台.cs文件里的方法,做一些JS无法做到的操作,如查询数据库     使用AjaxMethod要满足一下几点: 1.如 ...