iOS KVO 观察者模式实现方式

本文通过具体案例介绍了iOS中KVO(Key Value Observing)机制的使用方法,对比了不同观察对象属性变化的方法,并展示了如何利用KVO实现轻量级的数据响应更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://gaoyong.diandian.com/post/2012-08-08/40031793795

KVO (Key Value Observing)是ios里面一种特别方便的机制用于“捕捉”对象属性的变化。在概念理解上,是设计模式里面观察者模式的一种实践。

拿一个具体的例子来讲:

有一个数据对象EmployeeData,该对象有一个属性salary

有一个ViewController 用于显示对象EmployeeData的属性salary的值

当salary的值发生变化的时候,ViewController如何显示变化后的新值。

方案一:

“可以在EmployeeData类弱引用ViewController类,然后在EmployeeData数据发生变化时,调

用ViewController类的回调函数。这个方法虽然能达到目的,但是会破坏EmployeeData的完整

性,一个负责数据管理的类,不应当依赖另一个负责视图控制的类;换句话说,EmployeeData

类不应该知道关于ViewController类的任何事情,甚至不需要知道其从在”

方案二:

使用ios提供的消息中心(NSNotificationCenter)。在此EmployeeData为消息生产者,ViewController为消息消费者。当salary数据发生变化时发送一个消息既可。

ViewController接收到salary数据变化的通知做相对应的业务处理。不足之处如同方案一,EmployeeData对象的salary每次变动都需要发送“通知”。这项工作对EmployeeData自身来讲毫无意义。

方案三:

在ViewController中将自身(self)设置为EmployeeData salary属性值变化的观察者。当salary值发生变化时,执行一个回调方法。这样对“EmployeeData”来讲不用关心除自身业务以

外的事情。避免了方案一和方案二的瑕疵。对ViewController来讲关注谁的变化,注册自己为其的观察者既可。间接轻便。



下面是一些核心的代码。



//
// EmployeeData.h
// KVOExample
//
// Created by gaoyong on 12-8-8.
// Copyright (c) 2012年 gaoyong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface EmployeeData : NSObject {

NSString *salary;
}

@property(nonatomic,retain) NSString *salary;

@end



//
// EmployeeData.m
// KVOExample
//
// Created by gaoyong on 12-8-8.
// Copyright (c) 2012年 gaoyong. All rights reserved.
//

#import "EmployeeData.h"

@implementation EmployeeData
@synthesize salary;

@end



//
// ViewController.h
// KVOExample
//
// Created by gaoyong on 12-8-8.
// Copyright (c) 2012年 gaoyong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

UILabel *salary;
}

@property(nonatomic,retain) IBOutlet UILabel *salary;

@end





//
// ViewController.m
// KVOExample
//
// Created by gaoyong on 12-8-8.
// Copyright (c) 2012年 gaoyong. All rights reserved.
//

#import "ViewController.h"
#import "EmployeeData.h"


@interface ViewController () {

EmployeeData *employeeData;
}

@end

@implementation ViewController
@synthesize salary;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

employeeData = [[EmployeeData alloc] init];
[employeeData addObserver:self forKeyPath:@"salary" options:NSKeyValueObservingOptionNew context:nil];
}

-(void)viewDidAppear:(BOOL)animated {

//employeeData.salary = @"20";
//salary.text = employeeData.salary;
employeeData.salary = @"20";
employeeData.salary = @"200";
employeeData.salary = @"2000";
employeeData.salary = @"20000";
employeeData.salary = @"200000";
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

NSLog(@"observeValueForKeyPath is run"); // 美妙在这里:这一行会打印5次。

if (object == employeeData && [keyPath isEqualToString:@"salary"]) {

self.salary.text = employeeData.salary;
}
}

-(void) dealloc {

[employeeData removeObserver:self forKeyPath:@"salary"];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值