iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知。

在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification。

NSNotification 可以理解为消息对象,包含三个成员变量,如下:

@property (readonly, copy) NSString *name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;

name:通知的名称

object:针对某一个对象的通知

userInfo:字典类型,主要是用来传递参数

创建并初始化一个NSNotification可以使用下面的方法:

+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

NSNotificationCenter 通知中心,是一个单例。只能使用  [NSNotificationCenter defaultCenter] 获取通知中心对象。

发送通知可以使用下面的方法:

[[NSNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil];

或者:

NSDictionary *userInfo = @{@"name":@"John",@"age":@""};
NSNotification *notice = [NSNotification notificationWithName:@"testNotification" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notice];

添加一个观察者的方法(可以理解成在某个观察者中注册通知):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNoti:) name:@"testNotification" object:nil];

有四个参数:第一个参数是观察者,也就是谁注册这个通知;第二个参数是当接收到通知时的回调函数;第三个参数是通知的名称,通知名是区分各个通知的唯一标识;第四个参数是接收哪个对象发过来的通知,如果为nil,则接收任何对象发过来的该通知。

移除观察者,两种方法:

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

NSNotificationCenter 的使用举例如下:

发送通知(两种方法):

- (void)btnClicked
{
//[[NSNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil]; NSDictionary *userInfo = @{@"name":@"John",@"age":@""}; NSNotification *notice = [NSNotification notificationWithName:@"testNotification" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notice]; }

增加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNoti:) name:@"testNotification" object:nil];

回调函数(可以利用回调函数的参数 notification获得该通知的信息,比如name、object、参数):

- (void)haveNoti:(NSNotification *)notification
{
NSLog(@"name = %@",[notification name]); NSDictionary *userInfo = [notification userInfo];
NSLog(@"info = %@",userInfo);
}

针对某一个特定对象的通知的使用场景:

UITextField,比如说我们要监听UITextField 里面内容的变化,此时可以注册下面的通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchBarTextChange) name:UITextFieldTextDidChangeNotification object:self.searchBar];

self.searchBar 是 UITextField 类型。

iOS 通知中心 NSNotificationCenter的更多相关文章

  1. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  2. iOS基础 - 通知中心(NSNotificationCenter)

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

  3. IOS中通知中心(NSNotificationCenter)

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

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

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

  5. iOS通知中心

    iOS通知中心 它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信. 当通知中心接受到消息后会 ...

  6. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...

  7. iOS中通知中心(NSNotificationCenter)的使用总结

    一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...

  8. iOS通知中心升级 -可设置按优先级执行block

    简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...

  9. iOS 通知中心扩展制作初步-b

    涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...

随机推荐

  1. ASP.NET MVC 学习2、从Controller传递数据到View

      参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view 一,Control ...

  2. 【C#学习笔记】类型转换

    using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { " ...

  3. Java Socket(2): 异常处理

    1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...

  4. 【Unity3D】枪战游戏—发射子弹、射线检测

    一.子弹的碰撞检测: 因为子弹的移动速度非常的快,那么如果为子弹添加一个collider,就有可能检测不到了. 因为collider是每一帧在执行,第一帧子弹可能在100米处,那么下一帧就在900米处 ...

  5. 【转】Linux Posix Timer使用

    原文网址:http://blog.csdn.net/hongszh/article/details/8608781 最强大的定时器接口来自POSIX时钟系列,其创建.初始化以及删除一个定时器的行动被分 ...

  6. 一个响应式框架——agera

    Google在上周开源了一个响应式框架——agera,相信它会慢慢地被广大程序员所熟知.我个人对这样的技术是很感兴趣的,在这之前也研究过RxJava,所以在得知Google开源了这样的框架之后第一时间 ...

  7. [Everyday Mathematics]20150208

    对 $f\in C^2(\bbR)$ 适合 $$\bex \vlm{|x|}f(x)=0, \eex$$ 试证: $$\bex \int_{\bbR} |f'|^p\rd x \leq (p-1)^\ ...

  8. <转>详解DNS的常用记录(上):DNS系列之二

    详解DNS的常用记录(上) 在上篇博文中,我们介绍了DNS服务器的体系结构,从中我们了解到如果我们希望注册一个域名,那么必须经过顶级域名服务器或其下级的域名服务器为我们申请的域名进行委派,把解析权委派 ...

  9. MVC中modelstate的使用

    MVC中ModelState类需要引用 System.Web.Mvc命名空间,在 System.Web.Mvc.dll 中. 属性 Errors 返回一个 ModelErrorCollection 对 ...

  10. 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(二)

    第二步:登录到虚拟机 一旦创建好虚拟机后,默认azure会打开TCP 22端口,即SSH的端口.所以,我们可以通过远程连接,访问和管理该虚拟机. 首先,下载一个PuTTY软件.该软件很简单,就一个可执 ...