码迷,mamicode.com
首页 > 其他好文 > 详细

KVO监听

时间:2014-10-27 23:09:56      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   使用   for   on   log   cti   bs   

Main.m

#import "Children.h"
#import "Nurse.h"


int main(int argc, const char * argv[])
{
    
    Children *children = [[Children alloc] init];
    
    Nurse *nurse = [[Nurse alloc] initWithChildren:children];
    
    [[NSRunLoop currentRunLoop] run];
    
    [children release];
    [nurse release];
    
    return 0;
}

Children.h

@interface Children : NSObject {

    NSInteger _happyValue;
    
}

@property (nonatomic, assign) NSInteger happyValue; //欢乐值
@property (nonatomic, assign) NSInteger hungryValue;    //饥饿值

Children.m

@implementation Children

- (id)init {

    self = [super init];
    
    if (self) {
        //开启定时器
        [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(timeAction:)
                                       userInfo:nil
                                        repeats:YES];
        _happyValue = 100;
        _hungryValue = 100;
    }
    
    return self;
}

- (void)timeAction:(NSTimer *)time {

    NSInteger value1 = _happyValue;
    NSInteger value2 = _hungryValue;
    
    NSLog(@"happlValue:%ld",_happyValue);
    NSLog(@"_hungryValue:%ld",_hungryValue);
    
    //方式一:这样不能触发KVO,只有通过setter方法才能触发
//    _happyValue --;
    
    //new和old的界线是:调用setter方法
    
    //方式二:100 99  setter 99:错误,新旧值一样
//    [self setHappyValue:--_happyValue];
    
    //方式三:100 100  setter 99
    [self setHappyValue:--value1];
    [self setHungryValue:--value2];
    
    //KVC:也可以触发
//    [self setValue:[NSNumber numberWithInteger:--value] forKey:@"happyValue"];
    

    
}

Nurse.h

@class Children;

@interface Nurse : NSObject {

    Children *_chiledren;
    
}

- (id)initWithChildren:(Children *)children;

Nurse.m

#import "Children.h"

@implementation Nurse

- (id)initWithChildren:(Children *)children {

    self = [super init];
    
    if (self) {
        _chiledren = [children retain];
        
        //使用KVO监听小孩的属性happyValue变化
        [_chiledren addObserver:self
                     forKeyPath:@"happyValue"
                        options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                        context:NULL];
        
        //使用KVO监听小孩的属性hungryValue变化
        [_chiledren addObserver:self
                     forKeyPath:@"hungryValue"
                        options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                        context:NULL];
    }
    
    return self;
}

//小孩属性变化的时候,接收事件的方法
- (void)observeValueForKeyPath:(NSString *)keyPath  //观察的对象的属性名
                      ofObject:(id)object   //观察的对象
                        change:(NSDictionary *)change
                       context:(void *)context {

//    NSLog(@"change:%@",change);
    
    NSNumber *newNum = [change objectForKey:@"new"];
    int value = [newNum intValue];
    
    if ([keyPath isEqualToString:@"happyValue"]) {
        
        if (value < 90) {
            [self play];
        }
    }
    
    if ([keyPath isEqualToString:@"hungryValue"]) {
        
        if (value < 85) {
            NSLog(@"保姆给小孩喂饭");
            _chiledren.hungryValue = 100;
        }
    }
 
}


//陪小孩玩
- (void)play {

    NSLog(@"保姆和小孩玩");
    
    _chiledren.happyValue = 100;
    
}

- (void)dealloc {

    //移除观察者
    [_chiledren removeObserver:self forKeyPath:@"happyValue"];
    [_chiledren removeObserver:self forKeyPath:@"hungryValue"];
    
    [_chiledren release];
    
    [super dealloc];
    
}


KVO监听

标签:io   os   ar   使用   for   on   log   cti   bs   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/40515461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!