Objective-c语言_KVO

打开Xcode->选择IOS->Application->Single View Application->然后按next

2.如果你创建了工程那快捷键(com-shift-N)->选择IOS->Application->Single View Application->然后按next

在工程项下个找到ViewController.m文件

ViewController.m

#import "ViewController.h"

#import "Hero.h"

#import "Observe.h"


@interface ViewController ()

{

    Hero *hero;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //KVO

    /*

     

     KVO的由来:在编程的过程中。我们经常需要判断目标是否发生变化,以便及时的做出对应的处理。此时苹果公司

     就提供了一种策略,即'OC运行时'提供了'KVO'技术。其中'KVO'是基于‘KVC’实现的。

     

     KVO的实现:

     1.注册成观察者

     2.观察者定义LVO

     3.移除观察者

     

     

     */

    

    Hero *hero =[Heronew];

    [hero setValue:@100forKey:@"_HP"];//设置血量为100

    

    Observe *observe=[[Observealloc]initWithHero:hero];//创建观察者

    [hero setValue:@300forKey:@"_HP"];

    

    

    [NSTimerscheduledTimerWithTimeInterval:1.0

                                     target:self

                                   selector:@selector(change:)

                                   userInfo:nil

                                    repeats:YES];

    

    

    //计时器运行需要环境

    //没有环境为什么能运行

    //默认创建好的

    NSLog(@"main RunLoop =%@",[NSRunLoopmainRunLoop]);

    

    //所有如果在主线程中可以不用NSRunLoop做运行环境的语句,但是如果在非主线程中就要讲NSTimer加入

    //到该线程的NSRunLoop中,并启动NSRunLoop才行。

    //[[NSRunLoop currentRunLoop]run];

}


-(void)change:(NSTimer *)timer

{

    NSUInteger _curHP=[[herovalueForKey:@"_HP"]integerValue];//血量是封装好的

    _curHP -= 50;

    

    //血量为零时停止计时器

    if(_curHP<=0)

    {

        //[hero setValue:[NSNumber numberWithInteger:_curHP] forKey:@"_HP"];

        [hero setValue:@0forKey:@"_HP"];

        [timer invalidate];

    }

    else

    {

        [herosetValue:[NSNumbernumberWithInteger:_curHP]forKey:@"_HP"];

    }

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

=======================================================================
Hero.h

#import <Foundation/Foundation.h>


@interface Hero : NSObject

{

    NSUInteger _HP;

    //观察者监察血量

}

====================================================================
Hero.m

#import "Hero.h"


@implementation Hero


@end

=======================================================================
Observe.h

#import <Foundation/Foundation.h>

#import "Hero.h"


//观察者类

@interface Observe : NSObject


@property(nonatomic,retain)Hero *hero;


-(id)initWithHero:(Hero *)hero;


@end

===========================================================================
Observe.m

#import "Observe.h"


@implementation Observe


-(id)initWithHero:(Hero *)hero

{

    if (self = [superinit])

    {

        self.hero=hero;//英雄

        //注册监听者

        /*

         NSKeyValueObservingOptionNew

         NSKeyValueObservingOptionOld

         NSKeyValueObservingOptionInitial

         NSKeyValueObservingOptionPrior

         书本P225

         */

        [self.heroaddObserver:self

                    forKeyPath:@"_HP"

                       options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld//新值和旧值

                       context:nil];//创建监听者函数

        

    }

    return self;

}


//定义KVO回调

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    NSLog(@"change=%@",change);//change为字典

    NSLog(@"change=%@",change[NSKeyValueChangeNewKey]);

    

    /*

     change 是一个字典包含的key有如下

     

     NSKeyValueChangeKindKey;

     NSKeyValueChangeNewKey;   //新值

     NSKeyValueChangeOldKey;   //旧值

     NSKeyValueChangeIndexesKey;

     NSKeyValueChangeNotificationIsPriorKey

     

     

     书本P226

     */

   

}


//移除监听者

-(void)dealloc

{

    /*

     NSObject类定义了一个方法叫dealloc,当对象没有拥有着并内存要被回收是,系统

     会字典调用delloc方法,dealloc的责任是释放对自己的内存马师傅所有

     他控制的资源,包含释放所有实例变量的所以者。

     

     

     */

    

    [self.heroremoveObserver:selfforKeyPath:@"_HP"];

    NSLog(@"I am die");

        

}


@end



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值