您的位置:首页 > 其它

第三方库 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: