KVO监听

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值