您的位置:首页 > 移动开发 > IOS开发

iOS KVO的实现

2015-07-01 16:02 330 查看
kvo听说过,之前一直没怎么用,最近用到啦,就学习了一下。

demo介绍:有俩个viewcontroller(a和b),b里有个定时器和一个button,b push到a,a里的textview和label来展示b里属性str变化的值。

代码: 注:在哪add就在哪remove,viewDidLoad里add就dealloc里remove

bController.m

@property (strong,nonatomic)aController * aView;

@property (assign,nonatomic)int str;

- (void)viewDidLoad {

[super viewDidLoad];

self.aView=[[aController alloc]init];

self.str=0;

UIButton * btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame=CGRectMake(100, 100, 100, 100);

[btn setTitle:@"push" forState:UIControlStateNormal];

[btn setBackgroundColor:[UIColor orangeColor]];

[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

[btn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

[self addObserver:self.aView forKeyPath:@"str" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:nil];

}

//实现监听

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

{

if ([keyPath isEqual:@"str"]) {

NSLog(@"new str: %@",[change objectForKey:@"new"]);

NSLog(@"old str: %@",[change objectForKey:@"old"]);

}

}

-(void)push

{

NSTimer *myTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired)userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];

self.aView.str=self.str;

[self.navigationController pushViewController:self.aView animated:YES];

}

-(void)timerFired

{

self.str++;

}

aController.h

@property (assign,nonatomic)int str;

aController.m

@property (strong,nonatomic)UITextView * textView;

@property (strong,nonatomic)UILabel * label;

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor orangeColor];

[self addObserver:self forKeyPath:@"str" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew context:nil];

UITextView * textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 200)];

textView.backgroundColor=[UIColor whiteColor];

textView.textColor=[UIColor blackColor];

textView.editable=NO;

self.textView=textView;

[self.view addSubview:textView];

UILabel * label=[[UILabel alloc]init];

label.frame=CGRectMake(100, CGRectGetMaxY(textView.frame)+20, 100, 20);

label.backgroundColor=[UIColor orangeColor];

self.label=label;

[self.view addSubview:label];

}

//实现监听

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

{

if ([keyPath isEqual:@"str"]) {

[self writeToLog:[NSString stringWithFormat:@"%@",[change objectForKey:@"new"]]];

[self.label setText:[NSString stringWithFormat:@"%@",[change objectForKey:@"new"]]];

}

}

-(void)writeToLog:(NSString *)info{

static unsigned int count = 0;

[self.textView setText:[NSString stringWithFormat:@"[ %d ] %@\r\n%@",count,info,self.textView.text]];

count++;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: