KVO

3425 단어 VO
//  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


좋은 웹페이지 즐겨찾기