KVC 和KVO浅谈
一、KVC:Key-Value -Coding :直译为:键-值-代码;即:对键值进行改变的代码方法
二、KVO:Key-Value - Observe :直译为:键-值-观察;即:对键值对进行监视、观察的方法
KVO主要通过
1、设置观察者
2、观察内容发生变化触发的方法
A.设置将要监视的对象: // StockData.h #import <Foundation/Foundation.h> @interface StockData : NSObject
{
NSString *stockName;
NSString *price; }
@end
// StockData.m
#import "StockData.h" @implementation StockData @end B、编辑主界面
// MainViewController.h
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @end // MainViewController.m #import "MainViewController.h"
#import "StockData.h" @interface MainViewController () @property(nonatomic,retain)StockData *stockForKVO; @property(nonatomic,retain)UILabel *aLabel; @property(nonatomic,assign)NSInteger number; @end @implementation MainViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. self.number = ; _stockForKVO = [[StockData alloc]init]; [_stockForKVO setValue:@"searph" forKey:@"stockName"]; [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"]; [_stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL]; self.aLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; self.aLabel.textAlignment = NSTextAlignmentCenter;
self.aLabel.textColor = [UIColor orangeColor];
self.aLabel.text = [_stockForKVO valueForKey:@"price"];
[self.view addSubview:self.aLabel];
// [self.aLabel release]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(, , , ); [btn addTarget:self action:@selector(handleDown:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)handleDown:(UIButton *)sender{ _number ++; [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"price"]) { self.aLabel.text = [_stockForKVO valueForKey:@"price"]; } } - (void)dealloc{
[super dealloc];
[_stockForKVO removeObserver:self forKeyPath:@"price"];
[_stockForKVO release]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
KVC 和KVO浅谈的更多相关文章
- 谈KVC、KVO(重点观察者模式)机制编程
一不小心,小明在<跟着贝尔去冒险>这个真人秀节目中看到了“一日警察,一世警察”的Laughing哥,整个节目除了贝尔吃牛睾丸都不用刀叉的不雅餐饮文化外,还是镜头少普通话跟小明一样烂的Lau ...
- 漫谈 KVC 与 KVO
KVC 与 KVO 无疑是 Cocoa 提供给我们的一个非常强大的特性,使用熟练可以让我们的代码变得非常简洁并且易读.但 KVC 与 KVO 提供的 API 又是比较复杂的,绝对超出我们不经深究之前所 ...
- 浅谈iOS需要掌握的技术点
鉴于很多人的简历中的技术点体现(很多朋友问我iOS需要知道注意哪些)! 技术点: 1.热更新 (及时解决线上问题) 2.runtime(json解析.数据越界.扩大button点击事件.拦截系统方法) ...
- 11. KVC And KVO
1. KVC And KVO 的认识 KVC/KVO是观察者模式的一种实现 KVC全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用 ...
- iOS中关于KVC与KVO知识点
iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点 一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...
- 聊聊 KVC 和 KVO 的高阶应用
KVC, KVO 作为一种魔法贯穿日常Cocoa开发,笔者原先是准备写一篇对其的全面总结,可网络上对其的表面介绍已经够多了,除去基本层面的使用,笔者跟大家谈下平常在网络上没有提及的KVC, KVO进阶 ...
- iOS 自定义转场动画浅谈
代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...
- 浅谈 Fragment 生命周期
版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Fragment 文中如有纰漏,欢迎大家留言指出. Fragment 是在 Android 3.0 中 ...
- 浅谈 LayoutInflater
浅谈 LayoutInflater 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/View 文中如有纰漏,欢迎大家留言指出. 在 Android 的 ...
随机推荐
- Burp Suite Walkthrough(英文版)
Burp Suite is one of the best tools available for web application testing. Its wide variety of featu ...
- Firefly官方教程之DBentrust使用文档
原地址: http://bbs.gameres.com/thread_224185.html 1.dbentrust说明该模块主要是对数据库与memcached存储的处理.里面封装了从memcache ...
- IIS UrlWriter配置(asp.net)
前提在建虚拟目录或网站时注意以下设置第一步:下载URLRewriter 添加URLRewriter和ActionlessForm(不添加只能在VS实现,IIS下会找不到页面). 第二步:配置web.c ...
- 国内的一些开源镜像站汇总,EPEL源
国内的一些开源软件的镜像站 汇总 CentOS6.5安装 EPEL 源 国内高校的开源镜像站 中国科学技术大学(debian.ustc.edu.cn) 上海交通大学(ftp.stju.edu.c ...
- hadoop面试时可能遇到的问题
面试hadoop可能被问到的问题,你能回答出几个 ? 1.hadoop运行的原理? 2.mapreduce的原理? 3.HDFS存储的机制? 4.举一个简单的例子说明mapreduce是怎么来运行的 ...
- hadoop2.2编程: 数据压缩
本文主要讨论hadoop的数据压缩与解压缩代码的书写 Compressing and decompressing streams with CompressionCodec import org.ap ...
- bzoj2039
还是同一类最小割问题 对于只要记住,建图是来最小化损失, 最大化收益是所有收益-最小取不到的收益 首先对于每个经理i,如果不取,必然有signma(w[i,j])收益会得不到(这里我们先不考虑额外的损 ...
- apache开源项目--Cassandra
Apache Cassandra是一套开源分布式Key-Value存储系统.它最初由Facebook开发,用于储存特别大的数据.Facebook目前在使用此系统. 主要特性: 分布式 基于column ...
- Git submodule实战
http://blog.jqian.net/post/git-submodule.html 使用git管理的项目开发中,如果碰到公共库和基础工具,可以用submodule来管理. 常用操作 例如, 公 ...
- uiview scale
http://stackoverflow.com/questions/3946797/cgaffinetransformmakescale-makes-uiview-jump-to-original- ...