iOS KVC和KVO開發模式

KVC和KVO,KVC:即Key-Value-Coding而KVO:即Key-Value-Observer


    KVC是針對NSObject的子類,因爲它的實現是由於其括展類NSObject(NSKeyValueCoding),實現了

  -(void)setValue:(id)valueforKey:(NSString *)key;  

    - (id)valueForKey:(NSString *)key;這兩個方法的原因。


 上面的兩個方法不光針對屬性,對於類中的對象型變量同樣適用。


 而KVO也差不多,它是由於NSObject(NSKeyValueObserverRegistration)和NSObject(NSKeyValueObserving)的原因,用它可以實現回調功能下面是我寫的,下面的例子將其融合在一起

  

  

  1. #import "BIDViewController.h"  
  2. #import "FirstViewController.h"  
  3. @interface BIDViewController ()  
  4. {  
  5.     FirstViewController* first;  
  6.       
  7. }  
  8. @end  
  9.   
  10. @implementation BIDViewController  
  11.   
  12. - (void)viewDidLoad  
  13. {  
  14.     [super viewDidLoad];  
  15.     // Do any additional setup after loading the view, typically from a nib.  
  16.     first=[[FirstViewController alloc] init];  
  17.     //觀察FirstViewController中的變量name  
  18.     [first addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];  
  19. }  
  20.   
  21. - (void)didReceiveMemoryWarning  
  22. {  
  23.     [super didReceiveMemoryWarning];  
  24.     // Dispose of any resources that can be recreated.  
  25. }  
  26.   
  27. - (IBAction)clicked:(UIButton *)sender {  
  28.     [self presentViewController:first animated:YES completion:^{}];  
  29. }  
  30. //如果FirstViewController中的變量name的值變化執行下面  
  31. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context  
  32. {  
  33.     if([keyPath isEqualToString:@"name"])  
  34.     {  
  35.         NSLog(@"observer name is %@",[first valueForKey:@"name"]);  
  36.     }  
  37. }  
  38.   
  39. -(void)dealloc  
  40. {  
  41.     [self removeObserver:first forKeyPath:@"name"];  
  42.     [first release];  
  43.     [super dealloc];  
  44. }  
  45. @end  

 

  1. #import   
  2.   
  3. @interface FirstViewController : UIViewController  
  4. {  
  5.     NSString* name;  
  6. }  
  7. - (IBAction)clicked:(id)sender;  
  8. - (IBAction)otherClicked:(id)sender;  
  9.   
  10. @end  

 


 

  1. #import "FirstViewController.h"  
  2.   
  3. @interface FirstViewController ()  
  4. {  
  5. }  
  6. @end  
  7.   
  8. @implementation FirstViewController  
  9. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  10. {  
  11.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  12.     if (self) {  
  13.         // Custom initialization  
  14.     }  
  15.     return self;  
  16. }  
  17.   
  18. - (void)viewDidLoad  
  19. {  
  20.     [super viewDidLoad];  
  21.     // Do any additional setup after loading the view from its nib.  
  22. }  
  23.   
  24. - (void)didReceiveMemoryWarning  
  25. {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28. }  
  29.   
  30. - (IBAction)clicked:(id)sender {  
  31.     //當爲name設值時會執行BIDViewController中的觀察方法,就可進行回調了,不過其範圍是比不上真正意義上的回調  
  32.     [self setValue:@"chenliang" forKey:@"name"];  
  33.   
  34. //    [self dismissModalViewControllerAnimated:YES];  
  35.       
  36.       
  37. }  
  38.   
  39. - (IBAction)otherClicked:(id)sender {  
  40.       
  41.     NSLog(@"firstVC name is %@",[self valueForKey:@"name"]);  
  42. }  
  43. @end  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章