1. KVO全称Key-Value-Observing,也叫键值监听,是一种观察者设计模式.提供了一种机制,当指定的对象的属性被修改后,对象就会收到一个通知.也就是说每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者.
  2. 优势:可以降低两个类(业务逻辑和视图控制的类)之间的耦合性.也就是说可以很容易的实现视图组件和数据模型的分离.当数据模型的属性值改变之后作为监听器的视图组件就会被激发,激发时就会回调监听器自身.
  3. 在Objective-C中要实现KVO则必须实现NSKeyValueObServing协议.但不用担心,因为NSObject已经实现了该协议,因此几乎所有的Objective-C对象都可以使用KVO.
  1. 监听方法

    - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;

    某一个对象(受虐狂,喜欢被人监视),给自己添加一个监听者(一般都是控制器本身self),让监听者监听自身的某一个属性. options就是要求监听者记录的信息. context就是要监听者给自己添加一个标记,以防止和别的对象的监听混淆. 比如: 有两个孩子让家长监听他们做作业.监听者是家长,被监听的对象是两个孩子.

    参数:

    • observer 观察者,也就是KVO的订阅者,订阅者必须实现协议方法(下面有).
    • keyPath 描述将要观察的对象的属性,也就是被观察者的属性.
    • options KVO的属性配置.
      • NSKeyValueObservingOptionNewchange字典包括改变后的值
      • NSKeyValueObservingOptionOld change字典包括改变前的值
      • NSKeyValueObservingOptionInitial 注册后立刻触发KVO通知
      • NSKeyValueObservingOptionPrior 值改变前是否也要通知(这个key决定了是否在改变前改变后通知两次)
    • context上下文,这个会传递到协议方法中,用来区分消息,处理不同的KVO.所以应当是不同的.
  2. 解除监听

    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath; 删除指定keyPath的监听器.

    - (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(nullable void *)context NS_AVAILABLE(10_7, 5_0);删除特定上下文标记的指定keyPath的监听器.
  3. 回调监听

    - (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context;

    参数:

    • keyPath 被监听的keyPath
    • object 被监听的修改后的对象,可以获取修改的对象的属性
    • change 保存信息改变的字典(可能有旧的值,新的值等
    • context 上下文
  1. 注册KVO监听.
  2. 实现代理方法.
  3. 移除监听.在dealloc方法中移除.

非常重要

  1. 当你在同一个ViewController中添加多个KVO的时候,无论哪个KVO都是走- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context;回调方法.所以需要对想要的监听对象进行区分,以便指定不同的逻辑.

    这里是对_tableView对象的contentOffset 属性监听.
     if (object == _tableView && [keyPath isEqualToString:@"contentOffset"]) {
[self doSomething]; }
}```
2. 我们假设当前类(在例子中为UITableViewController)还有父类,并且父类也有自己绑定了一些其他KVO呢?我们看到,上述回调函数体中只有一个判断,如果这个if不成立,这次KVO事件的触发就会到此中断了。但事实上,若当前类无法捕捉到这个KVO,那很有可能是在他的superClass,或者super-superClass...中,上述处理砍断了这个链。合理的处理方式应该是这样的: ```- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == _tableView && [keyPath isEqualToString:@"contentOffset"]) {
[self doSomethingWhenContentOffsetChanges];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; }
}```
但是这个是要自己搞清楚,父类中到底有没有注册KVO.如果监听一个对象的两个属性,两个属性的改变时分开执行的,就会触发两次代理方法.如图: ![1.png](//upload-images.jianshu.io/upload_images/3424061-23984576dd247a4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3. KVO的一个特性,当对同一个`keyPath`进行多余一次的`removeObserver `的时候会导致程序crash.这种情况常常出现在父类有一个kvo,父类在dealloc中remove了一次,子类又remove了一次的情况下。不要以为这种情况很少出现!当你封装framework开源给别人用或者多人协作开发时是有可能出现的,而且这种crash很难发现.解决办法就是我们可以分别在父类以及本类中定义各自的context字符串,这样iOS就能知道移除的是自己的kvo,而不是父类中的kvo,避免二次remove造成crash.
4. 把监听到对象的属性值改变赋值的时候,一定要注意监听对象的值的类型.
把监听到对象的属性值改变赋值的时候,一定要注意监听对象的值的类型.
把监听到对象的属性值改变赋值的时候,一定要注意监听对象的值的类型.
**重要的事情说三遍**
5. 如果监听一个对象的多个属性,任何一个属性的改变都会走代理方法,也就是说对属性的监听,是分开执行的. > 全部代码 ![2.png](//upload-images.jianshu.io/upload_images/3424061-1c5afce7f3431292.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 1. MCBuyData.h
```#import <Foundation/Foundation.h>
@interface MCBuyData : NSObject
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, assign) NSInteger money;
@end```
2. MCBuyData.m
```#import "MCBuyData.h"
@implementation MCBuyData
@end```
3. ViewController.h
```#import <UIKit/UIKit.h>
@class MCBuyData;
@interface ViewController : UIViewController
@property (nonatomic, strong) MCBuyData * buyData;
@end```
4. ViewController.m ``` #import "ViewController.h" ```
```#import "Masonry.h"```
```#import "MCBuyData.h"```
```#define kNumber @"number"```
```#define kMoney @"money"```

@interface ViewController ()

@property (nonatomic, strong) UILabel * numberLabel;

@property (nonatomic, strong) UILabel * moneyLabel;

@property (nonatomic, strong) UIButton * toBuyButton;

@end

@implementation ViewController

  • (void)viewDidLoad {

    [super viewDidLoad];

    [self settingObserver];

    [self initUI];

    }
  • (void)dealloc {

    [self.buyData removeObserver:self forKeyPath:kNumber context:@"number"];

    [self.buyData removeObserver:self forKeyPath:kMoney context:@"money"];

    }

pragma mark - 点击事件

  • (void)toBuyButtonClicked {

    NSInteger number = [[self.buyData valueForKey:kNumber] integerValue];

    number += 1;

    [self.buyData setValue:@(number) forKey:kNumber];

    NSInteger money = [[self.buyData valueForKey:kMoney] integerValue];

    money += 100;

    [self.buyData setValue:@(money) forKey:kMoney];

    }

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

    // 该change的内容记录的是本次监听到的属性的改变.

    NSLog(@"change: %@",change);

    NSString * new = change[@"new"];

    if (object == self.buyData && [keyPath isEqualToString:kNumber] && (context == @"number")) {

    self.numberLabel.text = [NSString stringWithFormat:@"次数: %@",new];

    } else {

    // 写了这句,如果父视图中没有注册的KVO,就会崩掉.

    // reason: '<ViewController: 0x7fd7af406030>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.

    // [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

    if ([keyPath isEqualToString:kMoney]) {

    self.moneyLabel.text = [NSString stringWithFormat:@"金额: %@",new];

    } else {

    // [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

    }

    }
  • (void)settingObserver {

    self.buyData = [[MCBuyData alloc] init];

    [self.buyData setValue:@(0) forKey:kNumber];

    [self.buyData setValue:@(0) forKey:kMoney];

    [self.buyData addObserver:self forKeyPath:kNumber options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"number"];

    [self.buyData addObserver:self forKeyPath:kMoney options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"money"];

    }
  • (void)initUI {

    [self.view addSubview:self.numberLabel];

    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.mas_equalTo(self.view).with.offset(20);

    make.right.mas_equalTo(self.view).with.offset(-20);

    make.top.mas_equalTo(self.view).with.offset(100);

    make.height.mas_equalTo(50);

    }];

    [self.view addSubview:self.moneyLabel];

    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.mas_equalTo(self.view).with.offset(20);

    make.right.mas_equalTo(self.view).with.offset(-20);

    make.top.mas_equalTo(self.view).with.offset(250);

    make.height.mas_equalTo(50);

    }];

    [self.view addSubview:self.toBuyButton];

    [self.toBuyButton mas_makeConstraints:^(MASConstraintMaker *make) {

    make.left.mas_equalTo(self.view).with.offset(20);

    make.right.mas_equalTo(self.view).with.offset(-20);

    make.top.mas_equalTo(self.view).with.offset(400);

    make.height.mas_equalTo(50);

    }];

    }

pragma mark - setter & getter

  • (UILabel *)numberLabel {

    if (_numberLabel == nil) {

    self.numberLabel = [[UILabel alloc] init];

    self.numberLabel.backgroundColor = [UIColor orangeColor];

    self.numberLabel.font = [UIFont systemFontOfSize:15];

    self.numberLabel.textColor = [UIColor whiteColor];

    self.numberLabel.textAlignment = NSTextAlignmentCenter;

    self.numberLabel.text = @"次数: 1";

    } return _numberLabel;

    }
  • (UILabel *)moneyLabel {

    if (_moneyLabel == nil) {

    self.moneyLabel = [[UILabel alloc] init];

    self.moneyLabel.backgroundColor = [UIColor orangeColor];

    self.moneyLabel.font = [UIFont systemFontOfSize:15];

    self.moneyLabel.textColor = [UIColor whiteColor];

    self.moneyLabel.textAlignment = NSTextAlignmentCenter;

    self.moneyLabel.text = @"金额: 1";

    } return _moneyLabel;

    }
  • (UIButton *)toBuyButton {

    if (_toBuyButton == nil) {

    self.toBuyButton = [UIButton buttonWithType:UIButtonTypeCustom];

    self.toBuyButton.titleLabel.font = [UIFont systemFontOfSize:14];

    self.toBuyButton.backgroundColor = [UIColor redColor];

    [self.toBuyButton setTitle:@"买 买 买!!!" forState:UIControlStateNormal];

    [self.toBuyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [self.toBuyButton addTarget:self action:@selector(toBuyButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    } return _toBuyButton;

    }

    @end

> Demo 下载地址 <https://github.com/mancongiOS/KVO.git> > 说明 1. KVO注意事项1,2,3条转载于 编程小翁@博客园
05-27 08:35