在前一篇文章中我们介绍了OC中很常用的两个技术:KVC和KVO:http://blog.csdn.net/jiangwei0910410003/article/details/41912937,今天我们来看一下OC中另外的一个常用技术:通知(Nofitication)
其实这里的通知和之前说到的KVO功能很想,也是用于监听操作的,但是和KVO不同的是,KVO只用来监听属性值的变化,这个发送监听的操作是系统控制的,我们控制不了,我们只能控制监听操作,类似于Android中系统发送的广播,我们只能接受。但是通知就不一样了,他的监听发送也是又我们自己控制,我们可以在任何地方任何时机发送一个通知,类似于Android中开发者自己发送的广播。从这一点看来,通知的使用场景更为广泛了。
下面就来看一下例子:
还是护士和小孩的那个例子
Children.h
//
// Children.h
// 44_KVO
//
// Created by jiangwei on 14-10-16.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Children : NSObject
@property NSInteger *hapyValue;
@end
定义了一个属性:hapyValue
Children.m
//
// Children.m
// 44_KVO
//
// Created by jiangwei on 14-10-16.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import "Children.h"
@implementation Children
- (id) init{
self = [super init];
if(self != nil){
//启动定时器
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
self.hapyValue= 100;
}
return self;
}
- (void) timerAction:(NSTimer *) timer{
//使用set方法修改属性值,才能触发KVO
_hapyValue--;
NSLog(@"%@",_hapyValue);
if(_hapyValue <80){
//发送通知
//这里和KVO的区别,我们可以手动的发送通知
//注意通知的名称,传递的参数必须和定义通知的地方的参数值要一致
//将Children对象传递过去
[[NSNotificationCenter defaultCenter] postNotificationName:@"happyValueNotification" object:self];
}
}
@end
定义了一个定时器,但是我们看到这里的timerAction方法中就开始发送一个通知了:
//发送通知
//这里和KVO的区别,我们可以手动的发送通知
//注意通知的名称,传递的参数必须和定义通知的地方的参数值要一致
//将Children对象传递过去
[[NSNotificationCenter defaultCenter] postNotificationName:@"happyValueNotification" object:self];
我们在属性值发生变化的地方发送一个通知:NSNotificationCenter
第一个参数:通知的名称,这个名称必须和后面接受通知的名称一致
第二个参数:可以传递的一个参数对象
Nure.h
//
// Nure.h
// 44_KVO
//
// Created by jiangwei on 14-10-16.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Nure : NSObject
@end
Nure.m
//
// Nure.m
// 44_KVO
//
// Created by jiangwei on 14-10-16.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import "Nure.h"
#import "Children.h"
@implementation Nure
- (id) init{
self = [super init];
if(self != nil){
//监听一个通知,当收到通知时,调用notificationAction方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"happyValueNotification" object:nil];
}
return self;
}
- (void) notificationAction:(NSNotification *)notification{
//这里我们拿到Children对象进行操作
Children *children = notification.object;
NSLog(@"触发通知");
}
- (void)dealloc{
//移除指定的通知,不然会造成内存泄露
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"happyValueNotification" object:nil];
//Children对象可以添加多个通知
//下面的方法是可以移除Children中所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
在Nure类中我们开始接受通知了:
//监听一个通知,当收到通知时,调用notificationAction方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"happyValueNotification" object:nil];
使用addObserver方法来监听通知
第一个参数:监听者对象
第二个参数:监听处理逻辑的方法
第三个参数:通知的名称
第四个参数:通知发送的时候传递过来的参数对象
1、处理通知的方法
- (void) notificationAction:(NSNotification *)notification{
//这里我们拿到Children对象进行操作
Children *children = notification.object;
NSLog(@"触发通知");
}
这里会传递一个NSNotification对象,通过object属性可以获取到监听对象了,因为我们在发送通知的时候传递过来的这个对象。那么这里我们就可以获取监听对象的属性值了,但是这里我们如果想知道属性值变化前和变化后的值,我们可以在Children类中在定义一个属性专门用来记录旧的属性值,这样就可以了。
2、销毁方法
- (void)dealloc{
//移除指定的通知,不然会造成内存泄露
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"happyValueNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
在销毁的方法中,我们可以需要移除监听者,传递过去通知名
但是这里我们会注意到,还有一个方法:removeObserver方法,是用来移除所有监听者的,因为可能有多个监听者。
总结
OC中KVO操作和通知都是很重要的一个操作,他们的原理是基于观察者模式的,但是KVO操作没有通知灵活。但是KVO也有自己的优点,比如可以记录新旧值,这个通知就比较麻烦点了,所以我们在使用的时候视情况而定,一般监听属性值变化的我们还是使用KVO.
分享到:
相关推荐
OC-NSNotificationCenter.异步线程下载图片zip
iOS 通知 转发
iOS开发基础-NSNotificationCenter(通知中心)通知发布者和监听者示例,代码DEMO附带详细注释。有错误的地方欢迎提出。
iOS基础——通知代理之NSNotificationCenter、Delegate
iOS基础——通知代理之NSNotificationCenter、Delegate
通知中心(NSNotificationCenter)采用单例的模式,整个系统只有一个通知中心,通过如下代码获取: //获取通知中心 [NSNotificationCenter defaultCenter]; 注册通知监听器方法: //observer为监听器 //aSelector...
1.提供添加观察者、发送通知接口 2.封装类中已实现remove观察者 3.通过block回传发送者发送的信息
IOS中经常会使用到NSNotification和delegate来进行一些类之间的消息传递,这篇文章主要介绍了iOS NSNotificationCenter使用小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
该库的工作方式类似于Objective-C和Swift NSNotificationCenter也类似于Android平台中的BroadcastReceiver 。 通知分派机制,可将信息广播到注册的观察者。 文章 阅读Medium网站上的。 在C#角网站上阅读。 阅读...
NULL 博文链接:https://o0o0o0o.iteye.com/blog/645602
SFObservers, NSNotificationCenter和KVO自动删除观察者 在任何项目中,我不再使用 SFObservers,但仍然会接受请求的请求。命令行目SFObservers是在NSNotificationCenter和KVO中为观察者 Pattern 添加自动删除的类别...
详细说明了NSNotificationCenter的使用,包括各个参数的使用
IZEvent IZEvent是NSNotificationCenter的纯Swift替代方案。 它努力做到简单,安全和简单。 Pro的NSNotificationCenter上: 内存安全:不会发生内存泄漏,也不需要在deinit中删除观察者线程安全:默认情况下将事件...
A 页有N+1UIView,B页执行一个操作,A的N+1Uiview要修改,可以通过Delelgate实现,蛋有的时候无法实现时,选用消息模式
NULL 博文链接:https://andydufresneone.iteye.com/blog/1028462
发布通知方法 void postNotification(NSString * name) ; void postNotificationWithObject(NSString * name, id object) ; void postNotificationWithUserInfo(NSString * name, NSDictionary * userInfo) ; void ...
FXNotifications, 一个NSNotificationCenter的替代 API 命令行目FXNotifications是 NSNotificationCenter的一种类别,提供了基于基于的API,更简单易用,并避免了官方API的不同内存漏洞。有关更多详细信息,请参见...
一个简单的NSNotificationCenter的使用例子Demo,适合初学者。另外一个简单的UI效果是在tab bar的tab上面加上数字badge。 小编注:感谢开发者@Promise松 分享代码于本站。 Code4App编译测试,适用环境:Xcode ...