// BOY.h #import <Foundation/Foundation.h> @interface BOY : NSObject @property (nonatomic,retain) NSString * boyName; @end
// GIRL.h #import <Foundation/Foundation.h> @interface GIRL : NSObject @property (nonatomic,retain) NSString * girlName; @end
// RootViewController.m #import "RootViewController.h" #import "GIRL.h" #import "BOY.h" @interface RootViewController () { GIRL * xiaoGirl; BOY * xiaoBoy; } @end @implementation RootViewController -(void)createUI { NSArray * arr = @[@"BOY",@"GIRL"]; for(int i = 0;i<2;i++) { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(50 + 130 * i, 300, 100, 100); [btn setTitle:arr[i] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; btn.tag = i + 1; [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100 + 50 * i, 300, 40)]; label.backgroundColor = [UIColor greenColor]; label.tag = 100 + i; [self.view addSubview:label]; label.textAlignment = NSTextAlignmentCenter; [label release]; } } -(void)pressBtn:(id)sender { UIButton * btn = (UIButton *)sender; if(btn.tag == 1) { [xiaoBoy setValue:@"赵又廷" forKey:@"boyName"]; } else { [xiaoGirl setValue:@"高圆圆" forKey:@"girlName"]; } } - (void)viewDidLoad { [super viewDidLoad]; [self createUI]; xiaoGirl = [[GIRL alloc]init]; xiaoBoy = [[BOY alloc]init]; //通过KVC对成员属性进行赋值 [xiaoBoy setValue:@"黄晓明" forKey:@"boyName"]; [xiaoGirl setValue:@"Boby" forKey:@"girlName"]; //初始情况下label上的内容显示 UILabel * label1 = (UILabel *)[self.view viewWithTag:100]; UILabel * label2 = (UILabel *)[self.view viewWithTag:101]; label1.text = [xiaoBoy valueForKey:@"boyName"]; label2.text = [xiaoGirl valueForKey:@"girlName"]; //KVO //KVO 是 key-value-observer 键值观察者 //观察某个属性发生改变触发相应方法 //为BOY和GIRL的对象所包含的成员属性添加观察者 //1、添加观察者对象指针 谁观察成员变量的变化 就添加哪个对象指针 一般都是当前类的对象作为观察者 //2、添加的是被观察的成员属性名称 //3、被观察的成员属性变化的前后值 //4、直接设置为nil [xiaoBoy addObserver:self forKeyPath:@"boyName" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context: nil]; [xiaoGirl addObserver:self forKeyPath:@"girlName" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; //如果被观察的对象的成员属性发生改变 就会调用下面的方法 } -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { /* 1、被观察的成员属性的名称 2、被观察的成员属性所属的对象指针 3、存放的是被观察的成员属性变化的前后值(旧值的key是old 新值的key是new) */ UILabel * label1 = (UILabel *)[self.view viewWithTag:100]; UILabel * label2 = (UILabel *)[self.view viewWithTag:101]; //isMemberOfClass:判断对象指针是否属于某个类 if([object isMemberOfClass:[BOY class]]) { NSString * older = [change objectForKey:@"old"]; NSString * newer = [change objectForKey:@"new"]; NSString * string = [NSString stringWithFormat:@"old:%@ -- new:%@",older,newer]; label1.text = string; } else { NSString * older = [change objectForKey:@"old"]; NSString * newer = [change objectForKey:@"new"]; NSString * string = [NSString stringWithFormat:@"old:%@ -- new:%@",older,newer]; label2.text = string; } }