zoukankan      html  css  js  c++  java
  • KVO的概述的使用

    一,概述

    KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

    二,使用方法

    系统框架已经支持KVO,所以程序员在使用的时候非常简单。

    1. 注册,指定被观察者的属性,

    2. 实现回调方法

    3. 移除观察

    三,实例:

    假设一个场景,股票的价格显示在当前屏幕上,当股票价格更改的时候,实时显示更新其价格。

    1.定义DataModel,

     

    1. @interface StockData : NSObject {  
    2.     NSString * stockName;  
    3.     float price;  
    4. }  
    5. @end  
    6. @implementation StockData  
    7. @end  
    @interface StockData : NSObject {
        NSString * stockName;
        float price;
    }
    @end
    @implementation StockData
    @end

     

    2.定义此model为Controller的属性,实例化它,监听它的属性,并显示在当前的View里边

     

     
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.   
    5.     stockForKVO = [[StockData alloc] init];  
    6.     [stockForKVO setValue:@"searph" forKey:@"stockName"];  
    7.     [stockForKVO setValue:@"10.0" forKey:@"price"];      
    8.     [stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];  
    9.   
    10.     myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30 )];  
    11.     myLabel.textColor = [UIColor redColor];  
    12.     myLabel.text = [stockForKVO valueForKey:@"price"];  
    13.     [self.view addSubview:myLabel];  
    14.      
    15.     UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    16.     b.frame = CGRectMake(0, 0, 100, 30);  
    17.     [b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];  
    18.     [self.view addSubview:b];  
    19.   
    20. }  
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        stockForKVO = [[StockData alloc] init];
        [stockForKVO setValue:@"searph" forKey:@"stockName"];
        [stockForKVO setValue:@"10.0" forKey:@"price"];    
        [stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    
        myLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30 )];
        myLabel.textColor = [UIColor redColor];
        myLabel.text = [stockForKVO valueForKey:@"price"];
        [self.view addSubview:myLabel];
       
        UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        b.frame = CGRectMake(0, 0, 100, 30);
        [b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:b];
    
    }

    3.当点击button的时候,调用buttonAction方法,修改对象的属性

     

     
    1. -(void) buttonAction  
    2. {  
    3.     [stockForKVO setValue:@"20.0" forKey:@"price"];  
    4. }  
    -(void) buttonAction
    {
        [stockForKVO setValue:@"20.0" forKey:@"price"];
    }

     

    4. 实现回调方法

     

     
    1. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context  
    2. {  
    3.     if([keyPath isEqualToString:@"price"])  
    4.     {  
    5.         myLabel.text = [stockForKVO valueForKey:@"price"];  
    6.     }  
    7. }  
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if([keyPath isEqualToString:@"price"])
        {
            myLabel.text = [stockForKVO valueForKey:@"price"];
        }
    }

     

    5.增加观察与取消观察是成对出现的,所以需要在最后的时候,移除观察者

     

    1. - (void)dealloc  
    2. {  
    3.     [super dealloc];  
    4.     [stockForKVO removeObserver:self forKeyPath:@"price"];  
    5.     [stockForKVO release];  
    6. }  
    - (void)dealloc
    {
        [super dealloc];
        [stockForKVO removeObserver:self forKeyPath:@"price"];
        [stockForKVO release];
    }

     

    四,小结

    KVO这种编码方式使用起来很简单,很适用与datamodel修改后,引发的UIVIew的变化这种情况,就像上边的例子那样,当更改属性的值后,监听对象会立即得到通知。

  • 相关阅读:
    推荐算法学习资料
    imsdroid 学习(初认识)
    从网易新闻看离线阅读的实现思路
    关于PullToRefreshView bug 的修复
    Android Log日志的封装类,显示类名以及行号,快速定位
    Android Sqlite数据库版本升级管理初探
    《围观啦》发布了!!!!!!!
    单本书阅读,android客户端
    Android P2P语音通话实现(思路探讨)
    HTTP协议基础
  • 原文地址:https://www.cnblogs.com/gatsbywang/p/3518308.html
Copyright © 2011-2022 走看看