第三方库 KVOController 使用Demo
2016-03-09 15:43
267 查看
#import <Foundation/Foundation.h> @interface CurrentTimer : NSObject @property (nonatomic, strong) NSDate *date; @end
#import "CurrentTimer.h" @implementation CurrentTimer - (id)init { self = [super init]; if (self){ [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimer:) userInfo:nil repeats:YES]; } return self; } - (void)changeTimer:(NSTimer *)timer { self.date = [NSDate date]; } @end
------------------------------------------------------------------------------------------------------------------------------------------
#import <UIKit/UIKit.h> #import "CurrentTimer.h" @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UITextField *showDate; @property (strong, nonatomic) CurrentTimer *timer; @end
#import <UIKit/UIKit.h> #import "CurrentTimer.h" @interface ViewController : UIViewController @property (strong, nonatomic) IBOutlet UITextField *showDate; @property (strong, nonatomic) CurrentTimer *timer; @end
#import "ViewController.h" #import <FBKVOController.h> @interface ViewController () { FBKVOController *kvoController; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; kvoController = [FBKVOController controllerWithObserver:self]; self.timer = [[CurrentTimer alloc] init]; [kvoController observe:self.timer keyPath:@"date" options:NSKeyValueObservingOptionOld |NSKeyValueObservingOptionNew block:^(ViewController *view, CurrentTimer *timer, NSDictionary *change) { NSString *dateStr = [NSString stringWithFormat:@"%@", change[NSKeyValueChangeNewKey]]; self.showDate.text = dateStr; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
示例效果:
可参考文章: http://www.tuicool.com/articles/7J3Yru
相关文章推荐
- Android 中的Handler详解
- 那些年掉进的坑---内存踩踏实例记录
- Nodejs如何复制文件
- MySQL在线DLL测试
- android从网络获取图片直接存为bitmap
- android编译报错
- nodejs设置NODE_ENV环境变量
- HDU 4784 成都D题
- Activity之间切换动画
- Struts2 input
- Spring4.x 不再支持JpaTemplate和JpaDapSupport类了
- CentOS 6 配置公共key认证服务
- hibernate配置和JDBC连接池
- 缓存数据 ehcache
- xml(ibatis配置)中CDATA的用法
- 字符流中第一个不重复的字符
- Nginx支持php配置
- .net面试总结
- 作业一:(2)课程进度总结
- document.getElementsByTagName