KVC 和KVO淺談

1、KVC:Key-Value -Coding :直譯爲:鍵-值-代碼;即:對鍵值進行改變的代碼方法

 
該方法是OC(Object-C)爲咱們提供的一個不經過初始化方法而直接改變對象實例變量值的一種非正式Protocol的關鍵方法之一;
 
經過  [object setValue : value forKey : key];對該對象的對應的實例變量進行賦值操做,省略了初試化步奏,使用得當能夠大大省略代碼量;
 
 

2、KVO:Key-Value - Observe :直譯爲:鍵-值-觀察;即:對鍵值對進行監視、觀察的方法

 
該方法一樣是OC(Object-C)爲咱們提供的一個不經過初始化方法而直接改變對象實例變量值的一種非正式Protocol的關鍵方法之一;
 
KVO和KVC同爲基礎框架類方法,是之後開發,擴展所須要的基礎;
 

KVO主要經過

一、設置觀察者

 
[object addObserver: observer  forKeyPath:key  options:options  context:context];
 
其中
第一個參數:observer:觀察者,即擔任觀察者的對象。通常設爲工程所在界面自己。
 
第二個參數:key:鍵的路徑:即具體要監視的key所對應value的值
 
第三個參數:options:選項,預留參數,通常設置爲0;
 
第四個參數:context:上下文:通常設置爲Null;
 

二、觀察內容發生變化觸發的方法

 
- (void)observeValueForKeyPath:(NSString * )keyPath  ofObject:( id )object change:(NSDictionary * )change context:(void * )context{};
 
當所監視key的value發生變化時,自動觸發該方法;
 
三、注意編碼結束後應該先移去觀察者而後,才能釋放觀察者對象全部權,(沒有移除KVO或者過早移除都會形成崩潰);
 
3、代碼演示:(該代碼在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
相關文章
相關標籤/搜索