KVO---(Key-Value-Observer)键值观察者
KVO是一种观察者设计模式,是OC的机制,基于KVC.
KVO实现了一个类去观察另外一个类的某个属性或者多个属性的变化,从而做出相对应的反应.
下面是一个实例:
护士观察一个孩子是否醒来.
步骤:
//
// Child.h
// KVCDemo
//
// Created by 5016 on 13-12-19.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Child : NSObject
@property(assign,nonatomic)NSInteger sleep;
@property(retain,nonatomic)NSString* name;
@property(assign,nonatomic)NSInteger age;
-(Child*)initWithName:(NSString*)name andAge:(NSInteger)age andSleep:(NSInteger)sleep;
@end
//
// Child.m
// KVCDemo
//
// Created by 5016 on 13-12-19.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import "Child.h"
@implementation Child
@synthesize name = _name,age = _age,sleep=_sleep;
/*使用KVC可以间接访问类私有成员*/
-(Child*)initWithName:(NSString*)name andAge:(NSInteger)age andSleep:(NSInteger)sleep
{
if (self = [super init]) {
[self setValue:name forKey:@"name"];
[self setValue:[NSNumber numberWithInteger:age] forKey:@"age"];
[self setValue:[NSNumber numberWithInteger:sleep] forKey:@"sleep"];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
return self;
}
-(void)timerAction:(NSTimer *)timer
{
self.sleep -- ;
NSLog(@"sleep = %ld",self.sleep);
if (self.sleep == 80) {
[timer invalidate];
}
}
-(void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// Nurse.h
// KVCDemo
//
// Created by 5016 on 13-12-19.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
@class Child;
@interface Nurse : NSObject
@property(retain,nonatomic)NSString* name;
@property(assign,nonatomic)NSInteger age;
@property(assign,nonatomic)Child* child;
-(Nurse *)initWithName:(NSString*) name andAge:(NSInteger) age andChild:(Child*)child;
@end
//
// Nurse.m
// KVCDemo
//
// Created by 5016 on 13-12-19.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import "Nurse.h"
#import "Child.h"
@implementation Nurse
@synthesize child = _child,name = _name,age=_age;
-(Nurse *)initWithName:(NSString*) name andAge:(NSInteger) age andChild:(Child*)child
{
if (self = [super init]) {
[self setValue:name forKey:@"name"];
[self setValue:[NSNumber numberWithInteger:age] forKey:@"age"];
[self setValue:child forKey:@"child"];
//添加观察者
[self.child addObserver:self forKeyPath:@"sleep" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}
return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"孩子醒了%@",context);
}
-(void)dealloc
{
[self.child removeObserver:self forKeyPath:@"sleep"];//移除监听
[_child release];
[_name release];
[super dealloc];
}
@end
//
// main.m
// KVODemo
//
// Created by 5016 on 13-12-19.
// Copyright (c) 2013年 dradon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Nurse.h"
#import "Child.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Child *child = [[Child alloc] initWithName:@"dragon" andAge:23 andSleep:100];
Nurse *nurse = [[Nurse alloc] initWithName:@"xx" andAge:22 andChild:child];
[[NSRunLoop currentRunLoop]run];
}
return 0;
}