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++;
}
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++;
}
相关文章推荐
- iOS 自定义分享功能(分享到微博等sahreSDK)
- xCode升级 插件失效
- rommon模式下给路由器灌入IOS
- iOS button被view遮住,想点击怎么破
- iOS 设置最后一行的分割线边距为零
- iOS 截取屏幕当前显示的图片(可按范围截取)
- iOS开发_统计xcode代码行数
- iOS语言中的KVO机制
- 斯坦福大学ios公开课课程源代码下载地址
- iOS代码文档化,比注释更高级!
- 查看iOS错误日志
- iOS statusBar 状态栏定制
- IOS 给类目添加属性
- iOS绘图
- iOS8学习笔记2--autolayout
- iOS开发那些事-Git在Xcode中的配置与使用常见问题总结
- ios 本地通知
- iOS 6 自动布局 入门 2
- 截取字符串
- iOS view的frame和bounds之区别(位置和大小)