Objective-C语法之KVO的使用

来源:互联网 发布:与明星合照软件 编辑:程序博客网 时间:2024/04/27 12:14

简介:

上篇我们讲到了KVC,这篇我们学习KVO,全名为:Key Value Observing,直译为:基于键值的观察者。

那它有什么用呢?KVO主要用于视图交互方面,比如界面的某些数据变化了,界面的显示也跟着需要变化,那就要建立数据和界面的关联。

ObjC中提供的KVO就是解决这种问题的。以下用显示页面观察学生的课程名称变化的例子来说明KVO的使用。

学生类命名为:Student,页面类是:PageView.


上图来自苹果官网,图中的BankObject好比PageView,PersonObject好比Student,

PageView观察Student的变化。

1、添加Student学生类。

.h

[cpp] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2. @interface Student : NSObject  
  3. {  
  4.     NSString *name;  
  5.     NSString *courseName;  
  6. }  
  7. -(void)changeCourseName:(NSString*) newCourseName;  
  8. @end  

类中有name,和课程名称courseName,添加一个可以改变课程名称的方法changeCourseName。一会用来做对比,看直接改变课程名称时会不会有回调。

实现文件.m

[cpp] view plaincopy
  1. #import "Student.h"  
  2.   
  3. @implementation Student  
  4. -(void)changeCourseName:(NSString*) newCourseName  
  5. {  
  6.     courseName = newCourseName;  
  7. }  
  8. @end  

实现类把方法实现了。

2、页面类实现

.h文件

[cpp] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2. @class Student;  
  3. @interface PageView : NSObject  
  4. {  
  5.     Student *student;  
  6. }  
  7. -(id)init:(Student*)initStudent;  
  8. @end  

.m文件

[cpp] view plaincopy
  1. #import "PageView.h"  
  2. #import "Student.h"  
  3.   
  4. @implementation PageView  
  5. -(id)init:(Student*)initStudent  
  6. {  
  7.     if (self = [super init]) {  
  8.         student = initStudent;  
  9.         [student addObserver:self   
  10.                   forKeyPath:@"courseName"   
  11.                      options:NSKeyValueObservingOptionOld  
  12.                             |NSKeyValueObservingOptionNew context:nil];  
  13.     }  
  14.     return self;  
  15. }  
  16.   
  17. - (void) dealloc{   
  18.     [student removeObserver:self forKeyPath:@"courseName" context:nil];  
  19.     [super dealloc];   
  20. }  
  21.   
  22. -(void)observeValueForKeyPath:(NSString *)keyPath   
  23.                      ofObject:(id)object   
  24.                        change:(NSDictionary *)change   
  25.                       context:(void *)context  
  26. {  
  27.     if ([keyPath isEqual:@"courseName"]) {  
  28.         NSLog(@"PageView课程被改变了");  
  29.         NSLog(@"PageView新课程是:%@ 老课程是:%@", [change objectForKey:@"new"],[change objectForKey:@"old"]);  
  30.     }  
  31. }  
  32. @end  

init初始化时,向student实例添加观察者,在释放的时候移除观察者。

3、实现观察

在main函数中

[cpp] view plaincopy
  1. #import "Student.h"  
  2. #import "Course.h"  
  3. #import "PageView.h"  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.     @autoreleasepool {  
  8.         Student *student = [[[Student alloc]init]autorelease];  
  9.         [student changeCourseName:@"数学课"];  
  10.         NSLog(@"初始值:%@", [student valueForKey:@"courseName"]);  
  11.           
  12.         //创建页面实例  
  13.         PageView *pageview = [[[PageView alloc]init:student]autorelease];  
  14.           
  15.         [student setValue:@"化学课" forKey:@"courseName"];  
  16.         
  17.     }  
  18.     return 0;  
  19. }  
新建一个student的实例,设置他的课程是数学课,然后创建页面类的时候,用student初始化。这是页面类已经观察着学生的课程了。

再给课程设置新的值为化学课。这时候运行打印结果:

2012-07-24 16:29:21.561 objectiveC[2192:403] 初始值:数学课

2012-07-24 16:29:21.565 objectiveC[2192:403] PageView课程被改变了

2012-07-24 16:29:21.566 objectiveC[2192:403] PageView新课程是:化学课老课程是:数学课

可以看到Pageview类中的回调被调用,Pageview接收到学生课程数据更新的信息。

4、直接改变课程信息对比

[cpp] view plaincopy
  1. #import "Student.h"  
  2. #import "Course.h"  
  3. #import "PageView.h"  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.     @autoreleasepool {  
  8.         Student *student = [[[Student alloc]init]autorelease];  
  9.         [student changeCourseName:@"数学课"];  
  10.         NSLog(@"初始值:%@", [student valueForKey:@"courseName"]);  
  11.           
  12.         //创建页面实例  
  13.         PageView *pageview = [[[PageView alloc]init:student]autorelease];  
  14.           
  15.         [student setValue:@"化学课" forKey:@"courseName"];  
  16.         [student changeCourseName:@"英语课"];  
  17.         NSLog(@"直接改变的课程为:%@", [student valueForKey:@"courseName"]);  
  18.   
  19.     }  
  20.     return 0;  
  21. }  
直接调用changeCourseName方法改变课程,打印结果:

2012-07-24 16:32:06.230 objectiveC[2240:403] 初始值:数学课

2012-07-24 16:32:06.237 objectiveC[2240:403] PageView课程被改变了

2012-07-24 16:32:06.238 objectiveC[2240:403] PageView新课程是:化学课老课程是:数学课

2012-07-24 16:32:06.239 objectiveC[2240:403] 直接改变的课程为:英语课

可以看到,这时Pageview的回调没被调用到。说明只有通过键值编码(KVC)改变的值,才会回调观察者注册的方法。

这里是苹果官网的关于KVO的文档,英文好的朋友可以看看:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177-BCICJDHA

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

原创粉丝点击