14.键-值观察(KVO)

来源:互联网 发布:avscanner是什么软件 编辑:程序博客网 时间:2024/05/16 08:34

1.KVO的基本概念

  1. 键-值观察是一种使对象获取其他对象的特定属性变化的通知机制. 控制器层的绑定技术就是严重依赖键值观察获得模型层和控制器层的变化通知的. 对于不依赖控制器层类的应用程序, 键值观察提供了一种简化的方法来实现检查器并更新用户界面.
  2. 与NSNotification不同, 键-值观察并没有所谓的中心对象来为所有观察者提供变化通知. 取而代之地, 当变化发生时, 通知被直接发送至处于观察状态的对象. NSObject提供这种基础的键-值观察实现方法, 我们几乎不用重写该方法.
  3. 我们可以观察任意对象, 包括简单的属性, 对一或是对多关系. 对多关系的观察者将会被告知发生变化的类型-也就是任意发生变化的对象.
  4. 键-值观察为所有的对象提供自动观察兼容性. 我们可以通过自动观察通知并实现手动通知来筛选通知
  5. 注册观察者: 为了正确接受属性的变更通知, 观察者对象必须首先发送一个addObserver: forKeyPath: options: context: 消息至被观察对象, 用以传送观察对象和需要观察的属性的关键路径, 以便与其注册. 选项参数指定了发送变更通知时提供给观察者的信息. 使用NSKeyValueObservingOptionOld选项可以将初始化对象值变更字典中的一个项的形式提供给观察者. 指定NSKeyObservingOptionNew选项可以将新的值以一个项的形式添加至变更字典. 我们可以琢位”|”这两个常量来之指定接收上述两种类型的值.
  6. 接受变更通知: 当监听的属性发生变动时, 观察者收到observeValueForKeyPath: ofObject: change: context: 消息, 观察者必须实现这个方法. 触发观察通知的对象和键路径, 包含变更细节的字典, 以及观察者注册时提骄傲的上下文指针均提交给观察者.
  7. 移除观察者身份: 我们可以发送一条指定观察者对象和键路径的removeObserver: forKeyPath: 消息至被观察的对象, 来移除一个键-值观察者.(当我们达到目的时)

2.实例一:护士监听小孩

main.m文件

Child *child = [[Child alloc] init];Nurse *nurse = [[Nurse alloc] initWithChild:child];[[NSRunLoop currentRunLoop] run];

Child.h和Child.m文件

#import <Foundation/Foundation.h>@interface Child : NSObject@property(nonatomic,assign)NSInteger happyVal;@end
#import "Child.h"@implementation Child- (id)init {    self = [super init];    if (self != nil) {        self.happyVal = 100;        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];    }    return self;}- (void)timerAction:(NSTimer *)timer {    self.happyVal--;//点语法可以(Nurse中forKeyPath的参只能是happyVal)    //_happyVal--;//用KVC方法也可以(注意其中的forKey要和Nurse中forKeyPath的参数一样,要么都是happyVal,要么都是_happyVal)    //[self setValue:[NSNumber numberWithInteger:happyVal] forKey:@"_happyVal"];    //_happyValk--;//此方法不能触发KVO}@end

Nurse.h和Nurse.m文件

#import <Foundation/Foundation.h>@class Child;@interface Nurse : NSObject@property(nonatomic,retain)Child *child;- (id)initWithChild:(Child *)child;@end
#import "Nurse.h"#import "Child.h"@implementation Nurse- (id)initWithChild:(Child *)child {    self = [super init];    if (self != nil) {        _child = [child retain];        // self.child = child;//和上面等效        //注册观察者        [_child addObserver:self forKeyPath:@"happyVal" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"xxx"];    }    return self;}//接受变更通知- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    NSLog(@"%@",change);}- (void)dealloc {    //移除观察者    [_child removeObserver:self forKeyPath:@"happyVal"];    [_child release];    [super dealloc];}@end

3.实例二:监听文件拷贝进度

大文件拷贝,通过KVO监听已读的数据大小,实现拷贝的进度.
FileHandle.h和FileHandle.m文件

#import <Foundation/Foundation.h>@interface FileHandle : NSObject{@private    NSString *_srcPath;     //源文件路径    NSString *_tagetPath;   //目标文件路径}@property(nonatomic,assign)float filesize;    //文件的总大小@property(nonatomic,assign)float readedsize;  //已读的数据大小//定义两个拷贝的方法- (id)initPath:(NSString *)srcPath tagetPath:(NSString *)tagetPath;- (void)startCopy;@end
#import "FileHandle.h"@implementation FileHandle- (id)initPath:(NSString *)srcPath tagetPath:(NSString *)tagetPath {    self = [super init];    if (self != nil) {        _srcPath = [srcPath copy];        _tagetPath = [tagetPath copy];    }    return self;}- (void)startCopy {        NSFileManager *fileManager = [NSFileManager defaultManager];    //创建目标文件    BOOL success = [fileManager createFileAtPath:_tagetPath contents:nil attributes:nil];    if (success) {        NSLog(@"create success");    }    //读取文件对象    NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:_srcPath];    //写文件对象    NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:_tagetPath];    //获取文件的属性    NSDictionary *fileAttri = [fileManager attributesOfItemAtPath:_srcPath error:nil];    //获取文件的大小    NSNumber *fileSizeNum = [fileAttri objectForKey:NSFileSize];    BOOL isEnd = YES;    self.filesize = [fileSizeNum longValue];   //文件的总大小    NSAutoreleasePool *pool = nil;    int n = 0;    NSLog(@"开始复制....");    while (isEnd) {        if (n % 10 == 0) {            [pool release];            pool = [[NSAutoreleasePool alloc] init];        }        //计算剩下未读文件的大小        NSInteger subLeng = self.filesize - _readedsize;        NSData *data = nil;        //如果剩余文件大小小于5000,则将剩下的数据全都读完,并且不再循环读取了        if (subLeng < 5000) {            isEnd = NO; //跳出循环            data = [inFile readDataToEndOfFile]; //读取剩下的数据        } else {            //读取5000个字节            data = [inFile readDataOfLength:5000];            //累加读取的大小            // _readedsize += 5000;            self.readedsize += 5000;            [inFile seekToFileOffset:_readedsize];        }        //写入数据        [outFile writeData:data];        n++;    }    //关闭文件    [outFile closeFile];    NSLog(@"文件复制成功");}@end

User.h和User.m文件

#import <Foundation/Foundation.h>#import "FileHandle.h"@interface User : NSObject{@private    FileHandle *fileHandle;}- (void)doCopy;@end
#import "User.h"@implementation User- (id)init {    self = [super init];    if (self != nil) {        NSString *homePath = NSHomeDirectory();        NSString *srcPath = [homePath stringByAppendingPathComponent:@"svn配置.mov"];        NSString *tagetPath = [homePath stringByAppendingPathComponent:@"svn配置_bak.mov"];        fileHandle = [[FileHandle alloc] initPath:srcPath tagetPath:tagetPath];        //注册了属性的观察者        [fileHandle addObserver:self forKeyPath:@"readedsize" options:NSKeyValueObservingOptionNew context:nil];    }    return self;}- (void)doCopy{    [fileHandle startCopy];}- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    if ([keyPath isEqualToString:@"readedsize"]) {        NSNumber *readsizeNum = [change objectForKey:@"new"];        float readsize = [readsizeNum floatValue];        //fileHandle.filesize;//也可以通过ofobject拿到fileHandle        if ([object isKindOfClass:[FileHandle class]]) {            FileHandle *file = (FileHandle *)object;            float filesize = file.filesize;            float result = readsize/filesize * 100;            NSLog(@"%0.1f",result);        }    }}@end

main.m文件

User *user = [[User alloc] init];[user doCopy];

4.简单运用

声明一个Student类, 且写一条name属性, ViewController.m文件

#import "ViewController.h"#import "Student.h"@interface ViewController ()@property(nonatomic,retain)Student *stu;@end@implementation ViewController- (void)dealloc{    //如果添加了观察者,就一定要在这里移除观察者,不移除会造成内存问题    [self.stu removeObserver:self forKeyPath:@"name"];    [_stu release];    [super dealloc];}- (void)viewDidLoad {    [super viewDidLoad];    self.stu = [[Student alloc] init];    //KVC Key - Value - Coding   键值编码    //KVO Key - Value - Observer 键值观察    //监控对象里的属性值的变化,只有值发生了变化就会触发下面的方法    //注册一个监听者    [self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"这是监控的文本"];//context需要传送过去(触发方法里)的数据    //监听属性的值的变化,一定要用设置器,否则监听失效    self.stu.name = @"张三";    NSLog(@"测试");}- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{    //只要监听的属性的值发生变化了,就会马上触发这个方法    NSLog(@"%@",change);//change里存储了一些属性变化前后的数据}//先打印change的内容,然后才打印出"测试"
0 0
原创粉丝点击