KVO
2016-07-20 10:17
162 查看
KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。
简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
KVO的优点:
当有属性改变,KVO会提供自动的消息通知。这样开发人员不需要自己去实现这样的方案:每次属性改变了就发送消息通知。
这是KVO机制提供的最大的优点。因为这个方案已经被明确定义,获得框架级支持,可以方便地采用。
开发人员不需要添加任何代码,不需要设计自己的观察者模型,直接可以在工程里使用。
其次,KVO的架构非常的强大,可以很容易的支持多个观察者观察同 一个属性,以及相关的值。
使用步骤如下:
1. 注册,指定被观察者的属性,
2. 实现回调方法
3. 触发回调方法
4. 移除观察
KVO使用例子代码如下:
###############Model(模型)###############
#import
<Foundation/Foundation.h>
@interface Music : NSObject {
// 监听的属性
NSString *musicName;
}
@end
#import "Music.h"
@implementation Music
@end
###############ViewController(视图控制器)###############
#import <UIKit/UIKit.h>
@class Music;
@interface ViewController : UIViewController {
Music
*music;
}
@property (nonatomic, retain) Music *music;
@end
@implementation ViewController
@synthesize music;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super
viewDidLoad];
music =
[[Music alloc] init];
// 添加观察者
注册当属性发生改变的时候被调用的
[music addObserver:self
forKeyPath:@"musicName" options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld context:nil];
//
UILabel控件
UILabel
*musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150,
280, 21)];
musicLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
musicLabel.textColor = [UIColor redColor];
musicLabel.tag = 100;
[self.view
addSubview:musicLabel];
[musicLabel
release];
//
UITextField控件
UITextField
*musicTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,
200, 280, 21)];
musicTextField.font = [UIFont fontWithName:@"ArialMT"
size:18];
musicTextField.placeholder = @"Please enter some words.";
musicTextField.backgroundColor = [UIColor whiteColor];
//
UITextField输入内容时候调用
[musicTextField addTarget:self
action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:musicTextField];
[musicTextField release];
self.view.backgroundColor = [UIColor grayColor];
}
- (void)textFieldDidChange:(id)sender {
UITextField
*textField = (UITextField *)sender;
NSLog(@">>>>>>>>>>>>>>>%@",textField.text);
//
修改正在监听的属性,将调用下面回调方法
[music setValue:textField.text
forKey:@"musicName"];
}
// 只要Music类的"musicName"属性发生的变化都会触发到以下的方法
-
(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change context:(void
*)context {
UILabel
*label = (UILabel *)[self.view viewWithTag:100];
//
如果改变的属性是"musicName"
if
([keyPath
isEqualToString:@"musicName"]) {
// 将 当前的musicName属性的值
赋值给UILabel
label.text = [music
valueForKey:@"musicName"];
// 输出改变前的值
NSLog(@"old musicName is %@",[change
objectForKey:@"old"]);
// 输出改变后的值
NSLog(@"new musicName is %@",[change objectForKey:@"new"]);
}
}
#pragma mark - Memory Management
- (void)dealloc {
// 移除观察者
[music removeObserver:self
forKeyPath:@"musicName"];
[music
release];
[super
dealloc];
}
简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。
KVO的优点:
当有属性改变,KVO会提供自动的消息通知。这样开发人员不需要自己去实现这样的方案:每次属性改变了就发送消息通知。
这是KVO机制提供的最大的优点。因为这个方案已经被明确定义,获得框架级支持,可以方便地采用。
开发人员不需要添加任何代码,不需要设计自己的观察者模型,直接可以在工程里使用。
其次,KVO的架构非常的强大,可以很容易的支持多个观察者观察同 一个属性,以及相关的值。
使用步骤如下:
1. 注册,指定被观察者的属性,
2. 实现回调方法
3. 触发回调方法
4. 移除观察
KVO使用例子代码如下:
###############Model(模型)###############
#import
<Foundation/Foundation.h>
@interface Music : NSObject {
// 监听的属性
NSString *musicName;
}
@end
#import "Music.h"
@implementation Music
@end
###############ViewController(视图控制器)###############
#import <UIKit/UIKit.h>
@class Music;
@interface ViewController : UIViewController {
Music
*music;
}
@property (nonatomic, retain) Music *music;
@end
@implementation ViewController
@synthesize music;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super
viewDidLoad];
music =
[[Music alloc] init];
// 添加观察者
注册当属性发生改变的时候被调用的
[music addObserver:self
forKeyPath:@"musicName" options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld context:nil];
//
UILabel控件
UILabel
*musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150,
280, 21)];
musicLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
musicLabel.textColor = [UIColor redColor];
musicLabel.tag = 100;
[self.view
addSubview:musicLabel];
[musicLabel
release];
//
UITextField控件
UITextField
*musicTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,
200, 280, 21)];
musicTextField.font = [UIFont fontWithName:@"ArialMT"
size:18];
musicTextField.placeholder = @"Please enter some words.";
musicTextField.backgroundColor = [UIColor whiteColor];
//
UITextField输入内容时候调用
[musicTextField addTarget:self
action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:musicTextField];
[musicTextField release];
self.view.backgroundColor = [UIColor grayColor];
}
- (void)textFieldDidChange:(id)sender {
UITextField
*textField = (UITextField *)sender;
NSLog(@">>>>>>>>>>>>>>>%@",textField.text);
//
修改正在监听的属性,将调用下面回调方法
[music setValue:textField.text
forKey:@"musicName"];
}
// 只要Music类的"musicName"属性发生的变化都会触发到以下的方法
-
(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change context:(void
*)context {
UILabel
*label = (UILabel *)[self.view viewWithTag:100];
//
如果改变的属性是"musicName"
if
([keyPath
isEqualToString:@"musicName"]) {
// 将 当前的musicName属性的值
赋值给UILabel
label.text = [music
valueForKey:@"musicName"];
// 输出改变前的值
NSLog(@"old musicName is %@",[change
objectForKey:@"old"]);
// 输出改变后的值
NSLog(@"new musicName is %@",[change objectForKey:@"new"]);
}
}
#pragma mark - Memory Management
- (void)dealloc {
// 移除观察者
[music removeObserver:self
forKeyPath:@"musicName"];
[music
release];
[super
dealloc];
}
相关文章推荐
- Java中移位操作运算符的理解
- Wayland (二) 代码结构 [FW]
- Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
- gitlab 安装报错:Could not find modernizr-2.6.2 in any of the sources
- Codeforces Round #363 (Div. 2) 题解报告
- linux工作常用命令
- c++11多线程之主线程与子线程数据传输与共享
- Android Volley框架使用(三)
- JavaScript学习笔记二十三:浏览器对象
- Maven项目导入后报错
- Codeforces Round #363 (Div. 2) C. Vacations(简单动态规划)
- es6学习笔记11--Proxy和Reflect
- Cannot launch AVD in emulator
- Android内存泄漏的八种可能
- socket长连接、短连接以及心跳包机制
- Error ITMS-90635 invalid Mach-o format. the mach-o in hundle “*** ***.app/Frameworks/Result.framewo
- matlab一段音频取固定点数目
- android之自定义View和ViewGroup(五)(代码篇,实现类似竖着的ViewPager引导页,竖向引导页)
- jquery的几种异步请求,ajax
- Python包管理工具——Pip方法大全