监听者模式方便的监听所需要的值得变化
@implementation ViewController
- (IBAction)Buton:(id)sender {
self.view.backgroundColor=[UIColor redColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.model=[[Model alloc]init];
//第一个参数是 被监听者
//第二个参数是 监听者
//第三个参数是 监听内容
//第四个参数是 监听的状态变化
//
[self.view addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
[self.model addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
self.model.age=@"18";
self.model.age=@"12";
// Do any additional setup after loading the view, typically from a nib.
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"age"]) {
NSLog(@"01%@",keyPath);
NSLog(@"02%@",change);
NSLog(@"03%@",object);
NSLog(@"04%@",context);
}
}