• KVO


    //  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
    
  • 相关阅读:
    普通链表的各种排序及常用操作
    数据结构、算法与应用(C++描述)(第二版)第六章习题解答
    数据结构、算法与应用(C++描述)(第二版)第三章习题解答
    数据结构、算法与应用(C++描述)(第二版)第二章习题解答
    数据结构、算法与应用(C++描述)(第二版)第一章习题解答
    数据结构、算法与应用(C++描述)(第二版)第五章习题解答
    C++排序算法
    Code-C++-Cut CString to get keyValue by ","||"}"
    C++-Struct string初始化&&map初始化
    JSON的简单介绍以及C语言的JSON库使用
  • 原文地址:https://www.cnblogs.com/0515offer/p/4665465.html
Copyright © 2020-2023  润新知