KVO监听数据模型

监听一个model值得变化

    [self.model addObserver:self forKeyPath:@"note" options:NSKeyValueObservingOptionNew context:nil];

重写方法ObserveValueForkeypath方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    NSLog(@"keyPath %@",keyPath);
    NSLog(@"被修改的对象  %@",object);
    NSLog(@"被修改的值  %@",[change objectForKey:@"new"]);
    NSLog(@"被修改的上下文 %@",context);
}


移除

-(void)dealloc{
    [self.model removeObserver:self forKeyPath:@"note"];
}


你可能感兴趣的:(iOS学习积累)