Look,这是一个很简单的要求,点击Add me,age +1.

想一想的话很简单的,设置一个属性Nsinteger age,点击button add me,直接加1在重新显示Lable就好啦,不过,我们今天是来练习KVC和KVO的,苹果的键值编程哈哈

首先我们定义一个Human的类,并且完成了它的初始化方法

#import <Foundation/Foundation.h>

@interface human : NSObject

@property (nonatomic,assign) NSString *name;
@property (nonatomic,assign) NSInteger age;
-(id)initHuman:(NSString *)Initname hisage:(NSInteger) Initage;
@end
#import "human.h"

@implementation human
-(id)initHuman:(NSString *)Initname hisage:(NSInteger)Initage{
if(self=[super init]){
_name=Initname;
_age=Initage; }
return self;
} @end

很普通很简单。。。

下面在ViewControler中调用该类,完成对这个人的年龄的增加

#import <UIKit/UIKit.h>
#import "human.h"
@interface ViewController : UIViewController<UIWebViewDelegate>
{
human *keith;
}
@property (weak, nonatomic) IBOutlet UILabel *Infoshow;
- (IBAction)Addme:(id)sender;
@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
keith=[[human alloc]initHuman:@"keith" hisage:];
self.Infoshow.text=[NSString stringWithFormat:@"name is %@,age is %ld",keith.name,keith.age];
[keith addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];//
}
- (IBAction)Addme:(id)sender {
keith.age+=;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"age"] && object==keith) {
self.Infoshow.text=[NSString stringWithFormat:@"name is %@,age is %ld",keith.name,keith.age];//添加对keith keyPath的监视
}
}
-(void)dealloc{
[keith removeObserver:self forKeyPath:@"age"];//不用的时候移除注册
} @end

可以看出如果通过这样的模式有个好处是能减少模型和视图的耦合关键,模型只管运算,视图的变化通过KVO监视键值的变化,自动更新。

KVC && KVO 初见的更多相关文章

  1. KVC/KVO原理详解及编程指南

    一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...

  2. 【转】 KVC/KVO原理详解及编程指南

    原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 前言: 1.本文基本不讲KVC/KVO的用法,只结合网上的资料说说对这种技术的 ...

  3. kvc/kvo复习

    kvc/kvo复习 1 小问题 '[<XMGPerson 0x7fb8a8f30220> setValue:forUndefinedKey:]: this XMGPerson * pers ...

  4. 转:KVC/KVO原理详解及编程指南

      作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/9674431 转载请注明出处 如果觉得文章对你有所帮助,请通过留言或 ...

  5. 阶段性总结⓵触摸事件&手势识别⓶Quartz2D绘图⓷CALayer图层⓸CAAnimation⓹UIDynamic UI动力学⓺KVC&KVO

    知识点复习   1. 触摸事件&手势识别   1> 4个触摸事件,针对视图的 2> 6个手势识别(除了用代码添加,也可以用Storyboard添加)   附加在某一个特定视图上的, ...

  6. IOS开发之KVC KVO KVB

    KVC(Key Value Coding) KVO(Key Value Observing) KVB(Key Value Binding) KVO是Cocoa的一个重要机制,他提供了观察某一属性变化的 ...

  7. KVC & KVO

    KVC和KVO看上去又是两个挺牛的单词简写,KVC是Key-Value Coding的简写,是键值编码的意思.KVO是Key-Value  Observing的简写,是键值观察的意思.那么我们能拿KV ...

  8. KVC&&&KVO

    KVC 什么是KVC --->What KVC指的就是NSKeyValueCoding非正式协议. KVC是一种间接地访问对象的属性的机制. 这种间接表现在通过字符串来标识属性,而不是通过调用存 ...

  9. 04 KVC|KVO|Delegate|NSNotification区别

    一. iOS 中KVC.KVO.NSNotification.delegate 在实际的编程中运用的非常多,掌握好他们的运行原理和使用场合对于我们程序的开发将会带来事办工倍的效果:   二. KVC ...

随机推荐

  1. Unity3d-XML文件数据解析&amp;JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...

  2. 关于Java异常java.lang.OutOfMemoryError: PermGen space

    内容来源: http://blog.csdn.net/fengyie007/article/details/1780375 PermGen space的全称是Permanent Generation ...

  3. codeforces #550D Regular Bridge 构造

    题目大意:给定k(1≤k≤100),要求构造一张简单无向连通图,使得存在一个桥,且每一个点的度数都为k k为偶数时无解 证明: 将这个图缩边双,能够得到一棵树 那么一定存在一个叶节点,仅仅连接一条桥边 ...

  4. xcode7.1.1不能真机调试ios9.2系统设备的解决方法

    转载自:http://www.cocoachina.com/bbs/read.php?tid-331335.html 前些天手机升级到iOS9.2版本号  xcode7.1还能真机測试. 昨晚更新xc ...

  5. Python - pandas 数据分析

    pandas: powerful Python data analysis toolkit 官方文档: http://pandas.pydata.org/pandas-docs/stable/ 1. ...

  6. Failed to add reference to 'System.Net.Http'. Please make sure that it is in the Global Assembly Cache.

    关闭VS再来就好了

  7. codeblocks中右键源文件没有Rename选项?

    那是因为你右击的那个文件已经被CB的编辑器打开,关闭即可,你就能看到Rename选项了. 或者更简单,翻到Files那一栏,然后右击某个文件夹选择"Make root"即可,就跟w ...

  8. vs2015创建webService

  9. tomcat遇到版本问题

    1.Dynamic web module 2.J2EE  Web modules 3.Tomcat version 4.<web-app xmlns:xsi="http://www.w ...

  10. oracle怎么卸载

    Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.到目前仍在数据库市场上占有主要份额.劳伦斯·埃里森和他的朋友,之前的同事Bob ...