- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.p=[[Person alloc] init];
self.p.name=@"Kim";
self.p.age=@"18";
[self.p addObserver:self forKeyPath:@"name" options: NSKeyValueObservingOptionNew context:@"132"];
}
方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"keyPath:%@",keyPath);
NSLog(@"object:%@",object);
NSLog(@"changge:%@",change);
NSLog(@"context:%@",context);
self.view.backgroundColor=[UIColor redColor];
[self.p removeObserver:self forKeyPath:@"name"];
}