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 的 ...
随机推荐
- BZOJ 3971 Матрёшка 解题报告
很自然想到区间 DP. 设 $Dp[i][j]$ 表示把区间 $[i, j]$ 内的套娃合并成一个所需要的代价,那么有: $Dp[i][i] = 0$ $Dp[i][j] = min\{Dp[i][k ...
- 小米MIUI 360wifi掉线解决方案 落雨
问: 360WIFI 小米WIFI 联网无网速的解决办法 方法1.退出电脑和手机上的360安全卫士(我抱着试试的态度退出了之后,我艹,速度立马有了!),估计是这些安全软件太强大,导致的. 方法2.我用 ...
- 输出排名第k的法雷级数的值;
#include<cstdio> #include<cstring> using namespace std; int k,n; void dfs(int a,int b,in ...
- Bootstrap 貌似不错,先做一下记录
Bootstrap 简洁.直观.强悍的前端开发框架,让web开发更迅速.简单. http://www.bootcss.com/
- CDQ分治题目小结
CDQ分治属于比较特殊的一类分治,许多问题转化为这类分治的时候,时空方面都会有很大节省,而且写起来没有这么麻烦. 这类分治的特殊性在于分治的左右两部分的合并,作用两部分在合并的时候作用是不同的,比如, ...
- windows phone 中的TextBlock的一些特性(TextWrapping,TextWrapping)
文字很长时,换行:TextWrapping="Wrap" 文字很长时,省略:TextWrapping="WordEllipsis"
- MyGui 3.2.0(OpenGL平台)的编译(五篇文章)
MyGui是一个用来创建用户图形界面的库,用于游戏和3D应用程序.这个库的主要目标是达到:快速.灵活.易用. 1.下载准备: 源代码:http://svn.code.sf.net/p/my-gui/c ...
- 基于Qt5.x的QCA加解密开源项目的编译过程
记录一下Qt5.x下的QCA的编译过程 需要注意的几点 针对windows环境 1.编译QCA源码前,必须先安装openssl二进制文件 2.需要安装好vs2008等 ( 64-bit Visual ...
- Ubuntu10.10 安装scim
Ubuntu10.10 上没有找到默认的输入法,所以要安装一个中文输入法,网上好多介绍的,但都 不怎么好用,下面参考http://blog.csdn.net/caodesheng110/article ...
- Android:DES加密
private static final String KEY = "xxxxxx"; // KEY的字节长度必须超过24 public DESUtil(){ super(); } ...