Object-C 学习笔记(三十五)--- KVO(键值观察者)

15 篇文章 0 订阅

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值