ios_KVO 基本用法 代碼

@interface ViewController ()
@property(copy,nonatomic)NSString *str;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.str = @"1";
    [self addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}
- (IBAction)ss:(id)sender {
    self.str = @"2";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"%@,%@",keyPath,change);
}

舉一反三
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章