KVO,KVC,NSNotification

来源:互联网 发布:matlab 遍历打印数组 编辑:程序博客网 时间:2024/04/29 06:00

KVC

核心内容:

【对象 setValue aValue forKey aKey】;

对象的变量赋值

aValue = 【对象 valueForKey aKey】;//把变量值取出来


for example:


Student *stu = 【【Student alloc】init】;

【stu setValue:@"张三" forKey:@"name"】;//对对象变量赋值

NSString s1 = 【stu valueForKey:@"name"】;//变量值取出

【stu release】;




KVO:

1、注册监听

【被监听对象 addObserver 监听者 forKeyPath 被监听对象的属性 options监视内容(新值或旧值)context额外信息】;

2、监听者实现的方法

-(void)observerValueForKeyPath:(NSString*)keyPath ofObject(id)object (被监听对象) change(NSdictionary*)change context(void*)context

{

         NSLog(@“keyPath:%@ object:%@ change:%@ context:%@”,keyPath,object,     change,context);

}

3、监听触发条件

//被监听对象 key = newValue;

//自动调用监听方法

for example:


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

{

        NSLog:(@"keyPath:%@ object:%@ change:%@ context:%@ ",keyPath,object,change,context);

}


Student *stu = 【【Student alloc】init】;

stu.name = @"张三";

【stu addObserver self forKeyPath :@"name" options:NSKeyValueObservingOptionNew context:nil】;

stu.name = @"李四";

stu.name = @"王五";

【stu release】;

下面我们通过实际的代码来理解。这
段代码的业务含义就是警察一直监视犯人的名字是否发生变化,只要发生变化,警察就会收
到通知。
#import <Foundation/Foundation.h>
//犯人类型
@interface Prisoner: NSObject{
int pid;
NSString *name;
}
-(void) setPid: (int) pid;
-(void) setName: (NSString*) name;
-(int) pid;
-(NSString*) name;
@end
@implementation Prisoner
-(void) setPid: (int) p{
pid=p;
}
-(void) setName: (NSString*) n{
[n retain];
[name release];
name=n;
}
-(int) pid{
return pid;
}
-(NSString*) name{
return name;
}
-(void) dealloc{
[name release];
[super dealloc];
}
@end
//警察类型
@interface Police: NSObject
@end
@implementation Police
//接收通知的方法,继承自NSObject 父类。
//请先看main 函数中的addObserver 方法参数的解释再来这个方法的解释。
//第一个参数是你监视的对象上的属性,第二个参数是你监视的对象,第三个参数存放了你
监视的属性的值,最后一个参数我们传递nil。
- (void) observeValueForKeyPath: (NSString*) aPath
ofObject: (id) anObject
change: (NSDictionary*) aChange
context: (void*) aContext{
if([aPath isEqualToString: @"name"]){
NSLog(@"Police : %@",[aChange objectForKey: @"old"]);
NSLog(@"Police : %@",[aChange objectForKey: @"new"]);
//因为main 函数中我们监听name 的新旧两个值,所以aChange 这个字典对
象里就存放了@”old”、@”new”两个key-value 对。
}
}
@end
int main (int argc , const char * argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Prisoner *prisoner=[[Prisoner alloc] init];
Police *police=[[Police alloc] init];
//为犯人添加观察者警察,警察关注犯人的name 是否发生变化,如果发生变化就立即
通知警察,也就是调用Police 中的observeValueForKeyPath 方法。
//换句话说就是警察对犯人的名字很感兴趣,他订阅了对犯人的名字变化的事件,这个
事件只要发生了,警察就会收到通知。
[prisoner addObserver: police
forKeyPath: @"name"
options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context: nil];
//addObserver 的调用者是要被监视的对象,第一个参数是谁要监视它,第二个参数是
监视它的哪个属性的变化(使用KVC 机制,也就是前面所说的KVO 基于KVC),第三个参
数是监视属性值改变的类型,我们这里监听Old、New,也就是Cocoa 会把name 属性改变
之前的旧值、改变之后的新值都传递到Police 的处理通知的方法,最后一个参数我们传递
nil。
//这里有一个陷阱,如果你不小心把forKeyPath 的属性名写错了,prisoner 里根本就不
存在, 那么Cocoa 不会报告任何的错误。所以当你发现你的处理通知的
observeValueForKeyPath 没有任何反应的时候,首先看一下是不是这个地方写错了。
[prisoner setName: @"豆豆"];
//警察取消订阅犯人名字变化的事件。
[prisoner removeObserver: police
forKeyPath: @"name"];
[prisoner setName: @"太狼"];
[prisoner release];
[police release];
[pool release];
return 0;
}
运行之后,Shell 窗口输出:
2011-04-01 15:20:33.467 Kvo[2004] Police : <null>//因为name 没有old 值

2011-04-01 15:09:18.479 Kvo[4004] Police : 豆豆







NSNotification:

1、获得通知中心对象

NSNotificationCenter*nc = 【NSNotificationCenter defaultCenter】;

2、监听通知

【center addObserver :监听者 selector:须执行的方法 name:所监听者通知的名称 object:通知发送者】;

3、通知中心发布消息

【center PostNotificationName:@“国王万岁”object:某人】;

4.移除监听中心

for example:


-(void)test:(NSNtification*)n

{

     NSLog(@"name:%@ object:%@ userInfo:%@",【n name】,【n object】,【n userInfo】);

}



King *king = 【【King aloc】init】;

Farmer *farmer = 【【Farmer alloc】init】;

NSNotificationCenter *center = 【NSNotificationCenter defaultCenter】;

【center addObserver:farmer selector:@selector(test) name:@"国王万岁" object:king】;

【center postNotificationName:@"国王万岁" object:king】;

【center removeObserver:farmer】;

【king release】;

【farmer release】;

#import "Hero.h"


@implementation Hero

-(id)initWithName:(NSString *)theName Blood:(NSString *)theBlood{

   if (self = [superinit]) {

       _name = [theNameretain];

       _blood = [theBloodretain];

        

       //注册监听者

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(doChange:)name:@"changeHeroBlood"object:nil];

    }

    return self;

}

-(void)dealloc{

    //移除监听者

    [[NSNotificationCenterdefaultCenter]removeObserver:self];

    [_namerelease],_name =nil;

    [_bloodrelease],_blood =nil;

    [superdealloc];

}