全称是Key-value observing,键值观察机制。提供了一种当其它对象属性被修改的时候能通知当前对象的机制。server
在ObjC中使用KVO操做经常使用的方法以下:对象
- 注册指定Key路径的监听器: addObserver: forKeyPath: options: context:
- 删除指定Key路径的监听器: removeObserver: forKeyPath、removeObserver: forKeyPath: context:
- 回调监听: observeValueForKeyPath: ofObject: change: context:
KVO的使用步骤也比较简单:rem
- 经过addObserver: forKeyPath: options: context:为被监听对象(它一般是数据模型)注册监听器
- 重写监听器的observeValueForKeyPath: ofObject: change: context:方法
简单的说就是三步:io
1. 注册,指定被观察者的属性,方法
2. 实现回调方法数据
3. 移除观察vi