iOS KVC和KVO开发模式
2016-05-06 17:41
447 查看
KVC和KVO,KVC:即Key-Value-Coding而KVO:即Key-Value-Observer
KVC是针对NSObject的子类,因为它的实现是由于其括展类NSObject(NSKeyValueCoding),实现了
-(void)setValue:(id)valueforKey:(NSString *)key;
- (id)valueForKey:(NSString *)key;这两个方法的原因。
上面的两个方法不光针对属性,对于类中的对象型变量同样适用。
而KVO也差不多,它是由于NSObject(NSKeyValueObserverRegistration)和NSObject(NSKeyValueObserving)的原因,用它可以实现回调功能下面是我写的,下面的例子将其融合在一起
[plain] viewplaincopyprint?
#import "BIDViewController.h"
#import "FirstViewController.h"
@interface BIDViewController ()
{
FirstViewController* first;
}
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
first=[[FirstViewController alloc] init];
//观察FirstViewController中的变量name
[first addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(UIButton *)sender {
[self presentViewController:first animated:YES completion:^{}];
}
//如果FirstViewController中的变量name的值变化执行下面
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"name"])
{
NSLog(@"observer name is %@",[first valueForKey:@"name"]);
}
}
-(void)dealloc
{
[self removeObserver:first forKeyPath:@"name"];
[first release];
[super dealloc];
}
@end
[plain] viewplaincopyprint?
#import
@interface FirstViewController : UIViewController
{
NSString* name;
}
- (IBAction)clicked:(id)sender;
- (IBAction)otherClicked:(id)sender;
@end
[plain] viewplaincopyprint?
#import "FirstViewController.h"
@interface FirstViewController ()
{
}
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(id)sender {
//当为name设值时会执行BIDViewController中的观察方法,就可进行回调了,不过其范围是比不上真正意义上的回调
[self setValue:@"chenliang" forKey:@"name"];
// [self dismissModalViewControllerAnimated:YES];
}
- (IBAction)otherClicked:(id)sender {
NSLog(@"firstVC name is %@",[self valueForKey:@"name"]);
}
@end
KVC是针对NSObject的子类,因为它的实现是由于其括展类NSObject(NSKeyValueCoding),实现了
-(void)setValue:(id)valueforKey:(NSString *)key;
- (id)valueForKey:(NSString *)key;这两个方法的原因。
上面的两个方法不光针对属性,对于类中的对象型变量同样适用。
而KVO也差不多,它是由于NSObject(NSKeyValueObserverRegistration)和NSObject(NSKeyValueObserving)的原因,用它可以实现回调功能下面是我写的,下面的例子将其融合在一起
[plain] viewplaincopyprint?
#import "BIDViewController.h"
#import "FirstViewController.h"
@interface BIDViewController ()
{
FirstViewController* first;
}
@end
@implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
first=[[FirstViewController alloc] init];
//观察FirstViewController中的变量name
[first addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(UIButton *)sender {
[self presentViewController:first animated:YES completion:^{}];
}
//如果FirstViewController中的变量name的值变化执行下面
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"name"])
{
NSLog(@"observer name is %@",[first valueForKey:@"name"]);
}
}
-(void)dealloc
{
[self removeObserver:first forKeyPath:@"name"];
[first release];
[super dealloc];
}
@end
[plain] viewplaincopyprint?
#import
@interface FirstViewController : UIViewController
{
NSString* name;
}
- (IBAction)clicked:(id)sender;
- (IBAction)otherClicked:(id)sender;
@end
[plain] viewplaincopyprint?
#import "FirstViewController.h"
@interface FirstViewController ()
{
}
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)clicked:(id)sender {
//当为name设值时会执行BIDViewController中的观察方法,就可进行回调了,不过其范围是比不上真正意义上的回调
[self setValue:@"chenliang" forKey:@"name"];
// [self dismissModalViewControllerAnimated:YES];
}
- (IBAction)otherClicked:(id)sender {
NSLog(@"firstVC name is %@",[self valueForKey:@"name"]);
}
@end
相关文章推荐
- iOS各种权限状态获取
- iOS 多线程 - NSOperation
- iOS-键盘弹出的类型
- ios中常用数据类型相互转换
- iOS block的简单说明
- iOS中几种数据持久化方案
- iOS实现表格(非TableView)
- iOS7.0导航栏设置
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS开发AFNetworking的使用
- iOS 集成环信(三)
- 适配ios9出现的问题:-canOpenURL: failed for URL
- 浅谈iOS如何做弹幕
- iOS上架之内购
- iOS编码
- iOS 8 之后的动态沙盒路径
- iOS判断耳机是否插入
- iOS推送
- iOS 开发技巧
- iOS-OC中加载html5调用html方法和修改HTML5内容