//
// Person.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,retain)NSString * name;
@end
//
// Person.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// ViewController.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *texeField;
- (IBAction)didClickButton:(UIButton *)sender;
@property (nonatomic,retain)Person * person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] init];
// 1. 注册观察者(为被观察者添加一个观察者并设置观察的属性)
[_person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"hello"];
[_person release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
// 5.移除观察者
[_person removeObserver:self forKeyPath:@"name" context:@"hello"];
[_texeField release];
[super dealloc];
}
- (IBAction)didClickButton:(UIButton *)sender {
// 2.改变属性name的值
_person.name = _texeField.text;
}
// 3.只要是被观察者的属性值发生变化,就会执行下面的这个方法(这个方法会自动调用,不需要手动调用)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"被检测的对象的属性所在的路径:%@",keyPath);
NSLog(@"被观察者:%@",object);
NSLog(@"被观察的属性所在状态下的值:%@",change);
NSLog(@"在注册观察者的时候传过来的context的值:%@",context);
// 4. 取出新值和旧值(nil:空对象 @"":空字符串)
if (![change[@"new"] isEqualToString:change[@"old"]]) {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() %256/255.0 green:arc4random() %256/255.0 blue:arc4random() %256/255.0 alpha:1.0];
}
// 5.移除观察者(不写在这里,写在这里就不会有实现效果,应该写在dealloc里,即生命周期和person一致)
// [_person removeObserver:self forKeyPath:keyPath context:context];
}
@end
// Person.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,retain)NSString * name;
@end
//
// Person.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// ViewController.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *texeField;
- (IBAction)didClickButton:(UIButton *)sender;
@property (nonatomic,retain)Person * person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] init];
// 1. 注册观察者(为被观察者添加一个观察者并设置观察的属性)
[_person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"hello"];
[_person release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
// 5.移除观察者
[_person removeObserver:self forKeyPath:@"name" context:@"hello"];
[_texeField release];
[super dealloc];
}
- (IBAction)didClickButton:(UIButton *)sender {
// 2.改变属性name的值
_person.name = _texeField.text;
}
// 3.只要是被观察者的属性值发生变化,就会执行下面的这个方法(这个方法会自动调用,不需要手动调用)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"被检测的对象的属性所在的路径:%@",keyPath);
NSLog(@"被观察者:%@",object);
NSLog(@"被观察的属性所在状态下的值:%@",change);
NSLog(@"在注册观察者的时候传过来的context的值:%@",context);
// 4. 取出新值和旧值(nil:空对象 @"":空字符串)
if (![change[@"new"] isEqualToString:change[@"old"]]) {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() %256/255.0 green:arc4random() %256/255.0 blue:arc4random() %256/255.0 alpha:1.0];
}
// 5.移除观察者(不写在这里,写在这里就不会有实现效果,应该写在dealloc里,即生命周期和person一致)
// [_person removeObserver:self forKeyPath:keyPath context:context];
}
@end
使用观察者步骤和注意事项:
1:添加观察者
2.实现观察者方法
3.移除观察者(也是注意事项:使用完成后记得移除)