zoukankan      html  css  js  c++  java
  • Objective-C语法之KVO使用

     

    本文转自:http://blog.csdn.net/yuquan0821/article/details/6646400/

     

    一,概述

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

    二,使用方法

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

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

    2. 实现回调方法

    3. 移除观察

    三,实例:

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

    1.定义StockData

    #import <Foundation/Foundation.h>
    
    @interface StockData : NSObject{
        NSString *stockNmae;
        float price;
    }
    @end
    #import "StockData.h"
    
    @implementation StockData
    
    @end
    

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

    @interface ViewController ()
    @property(nonatomic,strong)StockData *stcokForKVO;
    @property(nonatomic,strong)UILabel *mylabel;
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.stcokForKVO=[[StockData alloc]init];
        [self.stcokForKVO setValue:@"searph" forKey:@"stockNmae"];
        [self.stcokForKVO setValue:@"10.0" forKey:@"price"];
        [self.stcokForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        
        self.mylabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
        self.mylabel.textColor=[UIColor redColor];
        self.mylabel.text= [NSString stringWithFormat:@"%@",[self.stcokForKVO valueForKey:@"price"]];
        [self.view addSubview:self.mylabel];
        
        UIButton *b=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        b.frame=CGRectMake(50, 50, 100, 30);
        b.backgroundColor=[UIColor blueColor];
        [b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:b];
        
    }
    


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

    -(void)buttonAction{
        [self.stcokForKVO setValue:[NSString stringWithFormat:@"%d", arc4random()%1000] forKey:@"price"];
    }

     4. 实现回调方法

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
        
        if ([keyPath isEqualToString:@"price"]) {
            self.mylabel.text= [NSString stringWithFormat:@"%@",[self.stcokForKVO valueForKey:@"price"]];
            NSLog(@"旧数据--%@--,新数据--%@--",[change objectForKey:@"old"],[change objectForKey:@"new"]);
        }
    
    }

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

    -(void)dealloc{
    
        [self.stcokForKVO removeObserver:self forKeyPath:@"price"];
    }

    四,小结

    KVO这种编码方式使用起来很简单,很适用与datamodel修改后,引发的UIVIew的变化这种情况,就像上边的例子那样,当更改属性的值后,监听对象会立即得到通知。
  • 相关阅读:
    ARM Linux 3.x的设备树(Device Tree)
    ubuntu 14.04 编译内核出现unable to locate package ncurses-devel 问题的解决
    Device Tree Usage( DTS文件语法)
    Ubuntu 14.04中gedit打开文件出现中文乱码问题
    Jenkins中集成jmeter-maven插件
    Linux(centos6.5)下安装jenkins
    IM系统架构设计之浅见
    一些常用软件的网络端口协议分类介绍
    Jenkins执行批处理文件失败
    八大持续集成工具
  • 原文地址:https://www.cnblogs.com/qianLL/p/5377644.html
Copyright © 2011-2022 走看看