KVo

原文:
http://blog.csdn.net/yuquan0821/article/details/6646400/

http://blog.csdn.net/sakulafly/article/details/14084183
1、注册了 price 属性
2、那么当属性的值被修改的时候
3、会得到通知
4、NSKeyValueObservingOptionNew 通过这个参数要求把新值在dictionary中传递过来
5、重写了observeValueForKeyPath:ofObject:change:context:方法,这个方法里的change这个NSDictionary对象包含了相应的值。

[change valueForKey:NSKeyValueChangeNewKey]

6、需要强调的是KVO的回调要被调用,属性必须是通过KVC的方法来修改的,如果是调用类的其他方法来修改属性,这个观察者是不会得到通知的。


使用方法
1、注册,指定被观察者的属性

//给某个对象 的某个key值设置监听
- (void)addObserver:(NSObject *)observer //谁来监听
forKeyPath:(NSString *)keyPath //监听属性
options:(NSKeyValueObservingOptions)options//监听操作 
context:(nullable void *)context;

2、实现回调方法

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context;

3、移除观察者

[_stockForKVO removeObserver:self forKeyPath:@"price"];

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

两个参数的意思是监听它的新值和旧值

NSKeyValueObservingOptionNew

NSKeyValueObservingOptionOld

原文:http://www.2cto.com/kf/201411/350907.html

实例:

//StockModel.h
#import 
@interface StockModel : NSObject
@property(nonatomic,strong)NSString *stockName;/**< 2 */
@property(nonatomic,strong)NSString *price;/**< 2 */
@end


//StockModel.m
#import "StockModel.h"
@implementation StockModel
@end

//ViewController.m

#import "ViewController.h"
#import "StockModel.h"
#import "Masonry.h"
@interface ViewController ()
@property(nonatomic,strong) StockModel *stockForKVO;/**< 股票 */
@property(nonatomic,strong) UILabel *stockPrice;/**< 股票价格 */
@property(nonatomic,strong) UIButton *changePrice;/**< 改变价格 */
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    _stockForKVO = [[StockModel alloc] init];
    [_stockForKVO setValue:@"searph" forKey:@"stockName"];
    [_stockForKVO setValue:@"10.0" forKey:@"price"];
    [_stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    
    
    
    [self createView];
    
    
    [self setViewData];
}

-(void)setViewData{
    self.stockPrice.text = [_stockForKVO valueForKey:@"price"];
}

-(void)createView{
    [self.stockPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
    [self.changePrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.stockPrice.mas_bottom);
        make.centerX.equalTo(self.view);
    }];
}

-(UILabel *)stockPrice{
    if (!_stockPrice) {
        _stockPrice = [UILabel new];
        [self.view addSubview:_stockPrice];
    }
    return _stockPrice;
}

-(UIButton *)changePrice{
    if (!_changePrice) {
        _changePrice = [UIButton new];
        [_changePrice setTitle:@"change" forState:UIControlStateNormal];
        [self.view addSubview:_changePrice];
        [_changePrice addTarget:self action:@selector(changePriceClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changePrice;
}

-(void)changePriceClick:(UIButton *)button{
    [_stockForKVO setValue:@"0.1" forKey:@"price"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([keyPath isEqualToString:@"price"])
    {
        self.stockPrice.text = [_stockForKVO valueForKey:@"price"];
    }
}

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

}

例子链接: https://pan.baidu.com/s/1eSFj0yu 密码: eh3f

你可能感兴趣的:(KVo)