iOS - KVO 简单应用
2024-09-28 20:52:41
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;
}
最新文章
- KnockoutJS 3.X API 第六章 组件(5) 高级应用组件加载器
- TDI - Transport Driver Interface
- windows下PHP+Mysql+Apache环境搭建
- 【原】画流程图工具visio使用技巧汇总
- redhat linux 安装mysql5.6.27
- View的setOnClickListener的添加方法
- VC中调用COM组件的方法(转载)
- 【转】 .NET中STAThread和MTAThread
- oracle12 pl/sql
- [Laravel 5 教程学习笔记] 一、Windows下安装Laravel 5
- git设置过滤忽略的文件或文件夹
- openlayers4 入门开发系列之聚合图篇(附源码下载)
- mac OSx 安装 mysqlclient
- 十五、Collections.sort(<;T>;, new Comparator<;T>;() {})针对字符串排序
- Java监听器Listener使用详解
- 将DataTable 覆盖到 SQL某表(包括表结构及所有数据)
- myql root用户远程访问
- PHP 5.5以上 使用 CURL 上传文件
- LeetCode——Search a 2D Matrix II
- Shell编程——bash基础知识
热门文章
- 分布式环境下的session管理
- 算法导论笔记——第二十章 van Emde Boas树
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法
- strTemp.Format (";%.*lf";,3,600.0);
- JavaScript高级程序设计学习笔记第九章--客户端检测
- css属性学习
- Fran&;ccedil;ois&;nbsp;Hollande’s&;amp;…
- java.endorsed.dirs的作用
- HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)
- Unity3d 5.3.5使用sqlite3