KVO

KVO的监听
Person* person=[person new];
person.title=@"你好";
[person addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
/** 添加观察者必须要实现的方法 */
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary )change context:(void )context{
/
打印新老值 */
// 从打印结果看 分别打印出了 name 与 height的新老值
NSLog(@"old : %@ new : %@",[change objectForKey:@"old"],[change objectForKey:@"new"]);
// NSLog(@"keypath : %@",keyPath);
// NSLog(@"change : %@",change);
}

你可能感兴趣的:(KVO)