KVO(键值监听)全称 Key Value Observing。使用KVO可以实现视图组件和数据模型的分离,视图作为监听器,当模型的属性值发生变化后,监听器可以做相应的处理。KVO的方法由NSKeyValueObserving协议提供,同样NSObject已经实现了该协议,因此几乎所有的对象都可以使用KVO。

使用KVO操作常用的方法如下:

注册制定路径的监听器: addObserver:  forKeyPath: option: context:

删除制定路径的监听器:removeObserver: forKeyPath:

触发监听时的方法:observeValueForKeyPath: ofObject: change: context:

 

引入的三方头文件:

#import "ReactiveObjC.h"
#import "MJExtension.h"

声明的属性:

@interface ViewController ()

@property(nonatomic, strong) UITextField *textField;
@property(nonatomic, strong) UILabel *lableRetValue; @end

实现方法:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.person = [[Person alloc] init];
[self.person addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionNew context:nil]; [self.textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
[self.person setValue:x forKey:@"str"];
}];
} -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"str"]) {
self.lableRetValue.text = [change valueForKey:@"new"];
}
} -(UILabel *)lableRetValue{
if (!_lableRetValue) {
_lableRetValue = [UILabel new];
_lableRetValue.layer.borderColor = [UIColor redColor].CGColor;
_lableRetValue.layer.borderWidth = .f;
_lableRetValue.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_lableRetValue]; [_lableRetValue mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset();
make.centerX.equalTo(self.view);
make.width.equalTo(self.view).multipliedBy(0.6);
make.height.offset();
}];
}
return _lableRetValue;
} -(UITextField *)textField{
if (!_textField) {
_textField = [UITextField new];
_textField.placeholder = @"请输入内容";
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.textAlignment = NSTextAlignmentCenter;
_textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textField]; [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.lableRetValue.mas_bottom).offset();
make.centerX.equalTo(self.view);
make.width.equalTo(self.view).multipliedBy(0.7);
make.height.offset();
}];
}
return _textField;
}

最新文章

  1. KnockoutJS 3.X API 第六章 组件(5) 高级应用组件加载器
  2. TDI - Transport Driver Interface
  3. windows下PHP+Mysql+Apache环境搭建
  4. 【原】画流程图工具visio使用技巧汇总
  5. redhat linux 安装mysql5.6.27
  6. View的setOnClickListener的添加方法
  7. VC中调用COM组件的方法(转载)
  8. 【转】 .NET中STAThread和MTAThread
  9. oracle12 pl/sql
  10. [Laravel 5 教程学习笔记] 一、Windows下安装Laravel 5
  11. git设置过滤忽略的文件或文件夹
  12. openlayers4 入门开发系列之聚合图篇(附源码下载)
  13. mac OSx 安装 mysqlclient
  14. 十五、Collections.sort(&lt;T&gt;, new Comparator&lt;T&gt;() {})针对字符串排序
  15. Java监听器Listener使用详解
  16. 将DataTable 覆盖到 SQL某表(包括表结构及所有数据)
  17. myql root用户远程访问
  18. PHP 5.5以上 使用 CURL 上传文件
  19. LeetCode——Search a 2D Matrix II
  20. Shell编程——bash基础知识

热门文章

  1. 分布式环境下的session管理
  2. 算法导论笔记——第二十章 van Emde Boas树
  3. java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法
  4. strTemp.Format (&quot;%.*lf&quot;,3,600.0);
  5. JavaScript高级程序设计学习笔记第九章--客户端检测
  6. css属性学习
  7. Fran&amp;ccedil;ois&amp;nbsp;Hollande’s&amp;amp;…
  8. java.endorsed.dirs的作用
  9. HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)
  10. Unity3d 5.3.5使用sqlite3