KVO
2015-07-21 19:59
211 查看
// StockModel.h // KVO // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import <Foundation/Foundation.h> @interface StockModel : NSObject @property (nonatomic,strong)NSString * stockName; @property (nonatomic,strong)NSString * price; /*.....*/ @end // StockModel.m // KVO // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import "StockModel.h" @implementation StockModel -(void)setValue:(id)value forUndefinedKey:(NSString *)key{ NSLog(@"UndefinedKey:%@",key); } @end
// ViewController.h // KVO // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end // // ViewController.m // KVO // // Created by 张国锋 on 15/7/20. // Copyright (c) 2015年 张国锋. All rights reserved. // #import "ViewController.h" #import "StockModel.h" @interface ViewController () { StockModel *_model; UILabel *_label; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //实例化一个股票,使用setValuesForKeysWithDictionary给所有属性赋值 _model=[StockModel new]; NSDictionary *dic=[NSDictionary dictionaryWithObjects:@[@"A股",@"10.0"] forKeys:@[@"stockName",@"price"]]; [_model setValuesForKeysWithDictionary:dic]; //_model.price=@"22"; /* 1、observer-发送通知的对象 2、keyPath-对象的key,需要观察的属性 3、options-告诉观察者观察的是什么 4、context-被观察者对象传⼊入的context */ [_model addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; //label 显示股票的价格 _label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 300, 300)]; _label.text=[_model valueForKey:@"price"]; _label.textAlignment=NSTextAlignmentCenter; _label.backgroundColor=[UIColor redColor]; [self.view addSubview:_label]; _label.font=[UIFont systemFontOfSize:50]; UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(10, 380, 300, 60)]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [btn setBackgroundColor:[UIColor greenColor]]; [btn setTitle:@"股票涨" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.view addSubview:btn]; } //change-包含变化之前与变化之后属性值的字典 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if ([keyPath isEqualToString:@"price"]) { _label.text=[_model valueForKey:@"price"]; NSLog(@"%@",change); } } -(void)btnClick{ NSLog(@"涨"); [_model setValue:@"20.0" forKey:@"price"]; _model.price=@"12"; //_label.text=@"123"; } -(void)dealloc{ [_model removeObserver:self forKeyPath:@"price"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Redis基本认识
- icvSetWeightsAndClasses
- 获取设置时间系统调用
- 应届生简历中常出现的问题
- Java 知识总结
- wpf 异步加载 只需6段代码
- 链表 插入排序
- HDU 5289 Assignment(2015多校训练)
- CoordinatorLayout与滚动的处理
- NSArray - 常用方法总结
- 两种视频播放形式和 视频截图
- [USACO 2012OPEN] Balanced Cow Subsets(meet in the middle + DFS)
- 大数模板
- 基于NodeJS进行前后端分离
- mongo配置副本集(replicaSet)
- Objective-C的对象模型和runtime机制
- 百练2755 神奇的口袋 背包水
- C语言 数组、函数应用实例篇
- Mac下修改环境变量
- Laravel 5基础教程(一)安装与部署 - 表严肃