zoukankan      html  css  js  c++  java
  • KVC 和KVO浅谈

    一、KVC:Key-Value -Coding :直译为:键-值-代码;即:对键值进行改变的代码方法

     
    该方法是OC(Object-C)为我们提供的一个不通过初始化方法而直接改变对象实例变量值的一种非正式Protocol的关键方法之一;
     
    通过  [object setValue : value forKey : key];对该对象的对应的实例变量进行赋值操作,省略了初试化步奏,使用得当可以大大省略代码量;
     
     

    二、KVO:Key-Value - Observe :直译为:键-值-观察;即:对键值对进行监视、观察的方法

     
    该方法同样是OC(Object-C)为我们提供的一个不通过初始化方法而直接改变对象实例变量值的一种非正式Protocol的关键方法之一;
     
    KVO和KVC同为基础框架类方法,是以后开发,扩展所需要的基础;
     

    KVO主要通过

    1、设置观察者

     
    [object addObserver: observer  forKeyPath:key  options:options  context:context];
     
    其中
    第一个参数:observer:观察者,即担任观察者的对象。一般设为工程所在界面本身。
     
    第二个参数:key:键的路径:即具体要监视的key所对应value的值
     
    第三个参数:options:选项,预留参数,一般设置为0;
     
    第四个参数:context:上下文:一般设置为Null;
     

    2、观察内容发生变化触发的方法

     
    - (void)observeValueForKeyPath:(NSString * )keyPath  ofObject:( id )object change:(NSDictionary * )change context:(void * )context{};
     
    当所监视key的value发生变化时,自动触发该方法;
     
    3、注意编码结束后应该先移去观察者然后,才能释放观察者对象所有权,(没有移除KVO或者过早移除都会造成崩溃);
     
    三、代码演示:(该代码在MRC环境下编辑)
     
      1 A.设置将要监视的对象:
      2 
      3 //  StockData.h
      4 
      5 #import <Foundation/Foundation.h>
      6 
      7 @interface StockData : NSObject
      8 {
      9     NSString *stockName;
     10    NSString  *price;
     11 
     12 }
     13 @end
     14 //  StockData.m
     15 #import "StockData.h"
     16 
     17 @implementation StockData
     18 
     19 @end
     20 
     21 
     22 B、编辑主界面
     23 //  MainViewController.h
     24 #import <UIKit/UIKit.h>
     25 
     26 @interface MainViewController : UIViewController
     27 
     28 @end
     29 
     30 //  MainViewController.m
     31 
     32 #import "MainViewController.h"
     33 #import "StockData.h"
     34 
     35 
     36 @interface MainViewController ()
     37 
     38 @property(nonatomic,retain)StockData *stockForKVO;
     39 
     40 @property(nonatomic,retain)UILabel *aLabel;
     41 
     42 @property(nonatomic,assign)NSInteger number;
     43 
     44 @end
     45 
     46 @implementation MainViewController
     47 
     48 - (void)viewDidLoad {
     49     [super viewDidLoad];
     50     // Do any additional setup after loading the view.
     51    
     52     self.number = 0;
     53    
     54     _stockForKVO = [[StockData alloc]init];
     55    
     56     [_stockForKVO setValue:@"searph" forKey:@"stockName"];
     57 
     58     [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"];
     59    
     60     [_stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
     61    
     62     self.aLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
     63    
     64     self.aLabel.textAlignment = NSTextAlignmentCenter;
     65     self.aLabel.textColor = [UIColor orangeColor];
     66     self.aLabel.text  = [_stockForKVO valueForKey:@"price"];
     67     [self.view addSubview:self.aLabel];
     68 //    [self.aLabel release];
     69    
     70     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     71    
     72     btn.backgroundColor = [UIColor whiteColor];
     73     btn.frame = CGRectMake(100, 30, 100, 30);
     74    
     75     [btn addTarget:self action:@selector(handleDown:) forControlEvents:UIControlEventTouchUpInside];
     76    
     77     [self.view addSubview:btn];
     78    
     79 }
     80 
     81 - (void)handleDown:(UIButton *)sender{
     82 
     83    
     84  
     85  
     86  
     87     _number ++;
     88    
     89     [_stockForKVO setValue:[NSString stringWithFormat:@"%ld",_number] forKey:@"price"];
     90 
     91 }
     92 
     93 
     94 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
     95 
     96     if ([keyPath isEqualToString:@"price"]) {
     97        
     98         self.aLabel.text = [_stockForKVO valueForKey:@"price"];
     99        
    100        
    101     }
    102    
    103    
    104 
    105 
    106 }
    107 
    108 
    109 - (void)dealloc{
    110     [super dealloc];
    111     [_stockForKVO removeObserver:self forKeyPath:@"price"];
    112     [_stockForKVO release];
    113 
    114 
    115 }
    116 
    117 
    118 - (void)didReceiveMemoryWarning {
    119     [super didReceiveMemoryWarning];
    120     // Dispose of any resources that can be recreated.
    121 }
    122 
    123 /*
    124 #pragma mark - Navigation
    125 
    126 // In a storyboard-based application, you will often want to do a little preparation before navigation
    127 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    128     // Get the new view controller using [segue destinationViewController].
    129     // Pass the selected object to the new view controller.
    130 }
    131 */
    132 
    133 @end
     
     
     
     
     
  • 相关阅读:
    mingw 构建 Geos
    nmake构建Geos库
    使用Dlib来运行基于CNN的人脸检测
    DLib Http Server程序示例
    DLib压缩解压程序示例
    GDAL添加ECW格式支持
    Dlib机器学习指南图翻译
    DLib库Base64编解码示例
    Dlib三维点云示例
    Mingw编译DLib
  • 原文地址:https://www.cnblogs.com/tig666666/p/4856330.html
Copyright © 2011-2022 走看看