iOS--KVO的概述与使用
KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
系统框架已经支持KVO,所以程序员在使用的时候非常简单。
1. 注册,指定被观察者的属性,
2. 实现回调方法
3. 移除观察

工程程序如下:
StockData.h
#import <Foundation/Foundation.h> @interface StockData : NSObject
{
NSString * stockName;
float price;
}
@end
StockData.m
#import "StockData.h"
@implementation StockData @end
这里定义属性是在ViewController.m文件里定义的,而ViewController.h里没有内容,故而没有列举出来。
ViewController.m
#import "ViewController.h"
#import "StockData.h"
@interface ViewController () @property(strong,nonatomic) UILabel *myLable;
@property(strong,nonatomic) StockData *stockforKVO; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.stockforKVO=[[StockData alloc] init];
[self.stockforKVO setValue:@"searph" forKey:@"stockName"];
[self.stockforKVO setValue:@"10.0" forKey:@"price"];
[self.stockforKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
self.myLable.textColor = [UIColor redColor];
self.myLable.text = [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
[self.view addSubview:self.myLable]; UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(, , , );
b.backgroundColor=[UIColor redColor];
[b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
-(void)buttonAction
{
// 点击按钮 切换数值
[self.stockforKVO setValue:[NSString stringWithFormat:@"%d",arc4random()%] forKey:@"price"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ if ([keyPath isEqualToString:@"price"]) {
self.myLable.text= [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
NSLog(@"旧数据--%@--,新数据--%@--",[change objectForKey:@"old"],[change objectForKey:@"new"]);
} } /*
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"price"])
{
self.myLable.text=[NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
}
}
*/ /**
* 移除观察者
*/
-(void)dealloc
{
[self.stockforKVO removeObserver:self forKeyPath:@"price"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
程序解析如下:
//StockData.h
#import <Foundation/Foundation.h> @interface StockData : NSObject
{
NSString * stockName;
float price;
}
@end
//StockData.m
#import "StockData.h"
@implementation StockData @end
- (void)viewDidLoad {
[super viewDidLoad];
self.stockforKVO=[[StockData alloc] init];
[self.stockforKVO setValue:@"searph" forKey:@"stockName"];
[self.stockforKVO setValue:@"10.0" forKey:@"price"];
[self.stockforKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
self.myLable = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
self.myLable.textColor = [UIColor redColor];
self.myLable.text = [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
[self.view addSubview:self.myLable];
UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(, , , );
b.backgroundColor=[UIColor redColor];
[b addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
}
3.当点击button的时候,调用buttonAction方法,修改对象的属性
-(void)buttonAction
{
// 点击按钮 切换数值
[self.stockforKVO setValue:[NSString stringWithFormat:@"%d",arc4random()%] forKey:@"price"];
}
4. 实现回调方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"price"]) {
self.myLable.text= [NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
NSLog(@"旧数据--%@--,新数据--%@--",[change objectForKey:@"old"],[change objectForKey:@"new"]);
}
}
/*
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"price"])
{
self.myLable.text=[NSString stringWithFormat:@"%@",[self.stockforKVO valueForKey:@"price"]];
}
}
*/
/**
* 移除观察者
*/
-(void)dealloc
{
[self.stockforKVO removeObserver:self forKeyPath:@"price"];
}

iOS--KVO的概述与使用的更多相关文章
- iOS KVO概述
iOS KVO概述 面试中经常会被问到:什么是KVO?这个问题既然出现概率这么大,那么我们就来详细讲一讲到底什么是KVO.下次再有面试官问你的时候,你就可以娓娓道来,以彰显高逼格 概述 问:什么是KV ...
- iOS:KVO/KVC 的概述与使用
iOS:KVO/KVC 的概述与使用 KVO APP开发技术QQ群:347072638 一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性 ...
- iOS Foundation 框架概述文档:常量、数据类型、框架、函数、公布声明
iOS Foundation 框架概述文档:常量.数据类型.框架.函数.公布声明 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业 ...
- iOS kvo 结合 FBKVOController 的使用
iOS kvo 结合 FBKVOController 的使用 一:FBKVOControlloer是FaceBook开源的一个 在 iOS,maxOS上使用 kvo的 开源库: 提供了block和@s ...
- kvo原理概述
kvo概述 kvo,全称Key-Value Observing,它提供了一种方法,当对象某个属性发生改变时,允许监听该属性值变化的对象可以接受到通知,然后通过kvo的方法响应一些操作. kvo实现原理 ...
- KVO的概述的使用
一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...
- iOS KVO & KVC
键值观察:值更改时通知观察者 键值观察(Key-value observing,或简称 KVO)允许对象观察另一个对象的属性.该属性值改变时,会通知观察对象.它了解新值以及旧值:如果观察的属性为对多的 ...
- KVO的概述与使用
一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知 ...
- iOS KVO详解
一.KVO 是什么? KVO 是 Objective-C 对观察者设计模式的一种实现.[另外一种是:通知机制(notification),详情参考:iOS 趣谈设计模式——通知]: KVO 提供一种机 ...
- iOS设计模式 - (1)概述
近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...
随机推荐
- [ASP.NET] 如果将缓存“滑动过期时间”设置为1秒会怎样?
今天编写了一个采用ASP.NET Caching的组件,在为它编写Unit Test的过程中发现了一个有趣的问题,接下来我通过一个简单的实例说明这个问题.我们在一个控制台应用中编写了如下一段程序,这个 ...
- 窥探Swift之别样的枚举类型
想必写过程序的童鞋对枚举类型并不陌生吧,使用枚举类型的好处是多多的,在这儿就不做过多的赘述了.Fundation框架和UIKit中的枚举更是数不胜数,枚举可以使你的代码更易阅读并且可以提高可维护性.在 ...
- geotrellis使用(十五)使用Bokeh进行栅格数据可视化统计
Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 实现方案 总结 一.前言 之前有篇文章 ...
- 开发中常用js记录(二)
(1)获得asp.net控件的value值 document.getElementById('<%=SUKid.ClientID %>').value (2)获得选中值 $('#selec ...
- 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。
(转载声明出处:http://www.cnblogs.com/linguanh/) 简介: 该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...
- JavaScript中typeof、toString、instanceof、constructor与in
JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定. 这也意味着你可以使用同一个变量保存不同类型的数据. 最新的 ECMAScrip ...
- 主机巡检脚本:OSWatcher.sh
主机巡检脚本:OSWatcher.sh 2016-09-26更新,目前该脚本只支持Linux操作系统,后续有需求可以继续完善. 注意: 经测试,普通用户执行脚本可以顺利执行前9项检查: 第10项,普通 ...
- JS去除空格方法记录
JS中去掉空格 //去除空格 String.prototype.Trim = function() { return this.replace(/\s+/g, ""); ...
- 基本的window.document操作及实例
基本的window.document操作及实例 找元素 1.根据id找 var d1 = document.getElementById("d1"); alert(d1); 2.根 ...
- spring boot启用tomcat ssl
首先要生成一个keystore证书.参考:Tomcat创建HTTPS访问,java访问https,ssl证书生成:cer&jks文件生成摘录,spring-boot 这里复现一下完整过程: 安 ...