iOS--KVO的概述与使用

1、概述

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

 
2、使用方法

系统框架已经支持KVO,因此程序员在使用的时候很是简单。框架

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

2. 实现回调方法ide

3. 移除观察编码

 
3、实例:
  假设一个场景,股票的价格显示在当前屏幕上,当股票价格更改的时候,实时显示更新其价格。
程序目录以下:
 

工程程序以下:atom

StockData.hspa


 

#import <Foundation/Foundation.h>

@interface StockData : NSObject
{
     NSString * stockName;
     float price;
}
@end

 StockData.m3d


 

#import "StockData.h"
@implementation StockData

@end

 

这里定义属性是在ViewController.m文件里定义的,而ViewController.h里没有内容,故而没有列举出来。code

ViewController.morm


#import "ViewController.h"
#import "StockData.h"
@interface ViewController ()

@property(strong,nonatomic) UILabel *myLable;
@property(strong,nonatomic) StockData *stockforKVO;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.stockforKVO=[[StockData alloc] init];
    [self.stockforKVO setValue:@"searph" forKey:@"stockName"];
    [self.stockforKVO setValue:@"10.0" forKey:@"price"];
    [self.stockforKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30 )];
    self.myLable.textColor = [UIColor redColor];
    self.myLable.text = [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
    [self.view addSubview:self.myLable];

    UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame = CGRectMake(0, 0, 100, 30);
    b.backgroundColor=[UIColor redColor];
    [b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b];
}
-(void)buttonAction
{
    // 点击按钮 切换数值
    [self.stockforKVO setValue:[NSString stringWithFormat:@"%d",arc4random()%1000] forKey:@"price"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"price"]) {
        self.myLable.text= [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
        NSLog(@"旧数据--%@--,新数据--%@--",[change objectForKey:@"old"],[change objectForKey:@"new"]);
    }
    
}

/*
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
     if([keyPath isEqualToString:@"price"])
    {
          self.myLable.text=[NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
    }
}
 */

/**
 *  移除观察者
 */
-(void)dealloc
{
    [self.stockforKVO removeObserver:self forKeyPath:@"price"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

程序解析以下:

1.定义DataModel,即本身定义的类
//StockData.h
#import
<Foundation/Foundation.h> @interface StockData : NSObject { NSString * stockName; float price; } @end

//StockData.m
#import "StockData.h"
@implementation StockData

@end
 
2.定义此model为Controller的属性,实例化它,监听它的属性,并显示在当前的View里边
- (void)viewDidLoad {
    [super viewDidLoad];
    self.stockforKVO=[[StockData alloc] init];
    [self.stockforKVO setValue:@"searph" forKey:@"stockName"];
    [self.stockforKVO setValue:@"10.0" forKey:@"price"];
    [self.stockforKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30 )];
    self.myLable.textColor = [UIColor redColor];
    self.myLable.text = [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
    [self.view addSubview:self.myLable];

    UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame = CGRectMake(0, 0, 100, 30);
    b.backgroundColor=[UIColor redColor];
    [b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b];
}

 

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

-(void)buttonAction
{
    // 点击按钮 切换数值
    [self.stockforKVO 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.myLable.text= [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
        NSLog(@"旧数据--%@--,新数据--%@--",[change objectForKey:@"old"],[change objectForKey:@"new"]);
    }
    
}

/*
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
     if([keyPath isEqualToString:@"price"])
    {
          self.myLable.text=[NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
    }
}
 */

 

5.增长观察与取消观察是成对出现的,因此须要在最后的时候,移除观察者
/**
 *  移除观察者
 */
-(void)dealloc
{
    [self.stockforKVO removeObserver:self forKeyPath:@"price"];
}

 

4、小结
 
KVO这种编码方式使用起来很简单,很适用与datamodel修改后,引起的UIVIew的变化这种状况,就像上边的例子那样,当更改属性的值后,监听对象会当即获得通知。
 
5、程序效果图
相关文章
相关标签/搜索