kvo
2013-05-07 20:13 甘超波 阅读(186) 评论(0) 编辑 收藏 举报#import <Foundation/Foundation.h> @interface NameObServer : NSObject @end #import "NameObServer.h" @implementation NameObServer -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"%@",keyPath); NSLog(@"%@",object); NSLog(@"%@",change); } @end ==================================== #import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic,copy) NSString *name; @end #import "Student.h" @implementation Student -(void)dealloc{ [_name release]; [super dealloc]; } @end =================================== #import <Foundation/Foundation.h> #import "Student.h" #import "NameObServer.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[[Student alloc] init] autorelease]; stu.name=@"mike"; NameObServer *nameobserve=[[[NameObServer alloc] init] autorelease]; //在给student的属性name添加观察对象 [stu addObserver:nameobserve forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; stu.name=@"jack"; //移除student的属性 name对象 [stu removeObserver:nameobserve forKeyPath:@"name"]; } return 0; }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频