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 的 ...
随机推荐
- 网页上PNG透明图片的ie6bug
只有IE6有这个Bug,所以的写法这样就可以了 #png{background:url(../images/png32.png) no-repeat;_filter:progid:DXImageTra ...
- jQuery中的一些正则匹配表达式
jQuery常用正则匹配表达式 落雨 //整数 "^-?[1-9]\\d*$", //正整数 "^[1-9]\\d*$", //负整数 intege2: &qu ...
- Ubuntu14.04建立WiFi热点
整理自Ubuntu 下建立WiFi热点的方法 Ubuntu14.04 Deepin2014分享WiFi 亲测成功 方法一:network manager 用Ubuntu自带的network manag ...
- 李洪强漫谈iOS开发[C语言-015]-变量的使用
- Java Vector 类
Vector类实现了一个动态数组.和ArrayList和相似,但是两者是不同的: Vector是同步访问的. Vector包含了许多传统的方法,这些方法不属于集合框架. Vector主要用在事先不知道 ...
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-008-SpEL介绍
一. 1.SpEL expressions are framed with #{ ... } 2.SpEl的作用 Sp EL has a lot of tricks up its sleeves, ...
- Spring事务传播机制
Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播,即协调已经有事务标识的方法之间的发生调用时的事务 ...
- Myeclipse开发内存溢出问题
MyEclipse开发内存溢出问题 window --> preferences --> MyEclipse --> servers --> Tomcat --> J ...
- C++ CGI Helloword
一 什么是CGI CGI(The Common Gateway Interface):通用网关接口,定义web服务器和客户脚本进行信息交互的一系列标准. 二 web浏览器 为了了解CGI的概念,让我 ...
- ORACLE RAC NTP 时间服务器配置
Linux 时间同步配置 . 一. 使用ntpdate 命令 1.1 服务器可链接外网时 # crontab -e 加入一行: */1 * * * * ntpdate 210.72.145.44 21 ...