KVO

//  StockModel.h
//  KVO
//
//  Created by 张国锋 on 15/7/20.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface StockModel : NSObject
@property (nonatomic,strong)NSString * stockName;
@property (nonatomic,strong)NSString * price;
/*.....*/
@end


//  StockModel.m
//  KVO
//
//  Created by 张国锋 on 15/7/20.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "StockModel.h"

@implementation StockModel

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"UndefinedKey:%@",key);
}

@end
//  ViewController.h
//  KVO
//
//  Created by 张国锋 on 15/7/20.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end


//
//  ViewController.m
//  KVO
//
//  Created by 张国锋 on 15/7/20.
//  Copyright (c) 2015年 张国锋. All rights reserved.
//

#import "ViewController.h"
#import "StockModel.h"
@interface ViewController ()
{
    StockModel *_model;
    UILabel *_label;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //实例化一个股票,使用setValuesForKeysWithDictionary给所有属性赋值
    _model=[StockModel new];
    NSDictionary *dic=[NSDictionary dictionaryWithObjects:@[@"A股",@"10.0"] forKeys:@[@"stockName",@"price"]];
    
    [_model setValuesForKeysWithDictionary:dic];
    //_model.price=@"22";
    /*
     1、observer-发送通知的对象
     2、keyPath-对象的key,需要观察的属性
     3、options-告诉观察者观察的是什么
     4、context-被观察者对象传⼊入的context

     */
    [_model addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
    //label 显示股票的价格
    _label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 300, 300)];
    _label.text=[_model valueForKey:@"price"];
    _label.textAlignment=NSTextAlignmentCenter;
    _label.backgroundColor=[UIColor redColor];
    [self.view addSubview:_label];
    _label.font=[UIFont systemFontOfSize:50];
    
    
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 380, 300, 60)];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:[UIColor greenColor]];
    [btn setTitle:@"股票涨" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
}
//change-包含变化之前与变化之后属性值的字典
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"price"]) {
        _label.text=[_model valueForKey:@"price"];
        NSLog(@"%@",change);
    }
}


-(void)btnClick{
    NSLog(@"涨");
    [_model setValue:@"20.0" forKey:@"price"];
    _model.price=@"12";
    //_label.text=@"123";
}

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


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
原文地址:https://www.cnblogs.com/0515offer/p/4665465.html