2常用的KVO、KVC、Timer
来源:互联网 发布:中国人审美 知乎 编辑:程序博客网 时间:2024/06/07 18:19
KVO、KVC、Timer是IOS以及Android中经常使用的技术
下面分别从OC、SWIFT、JAVA三个方面阐述它的使用
OC:kvc在使用的时候在另外一个类中需要有它的属性定义
// 添加属性监听
-(void)testKVO{
tc = [[testclass alloc]init];
tc.ttl = @"123";
[tcaddObserver:selfforKeyPath:@"ttl"options:NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOldcontext:@"xxx"];
}
// 添加监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object change:(NSDictionary *)change
context:(void *)context {
NSLog(@"%@",change);
NSLog(@"%@",context);
}
-(void)dealloc{
[tcremoveObserver:selfforKeyPath:@"ttl"];
}
#pragma -mark KVC
-(void)testKVC{
[tcsetValue:@"123"forKey:@"keytc"];
NSLog(@"%@",[tcvalueForKey:@"keytc"]);
}
#pragma -mark Timer
-(void)testTimer{
NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(checkUnreadCount)userInfo:nilrepeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)checkUnreadCount{
NSLog(@"HHHYYY");
}
//// 2.监听textView文字改变的通知
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
Swift:overridefunc viewDidLoad() {
super.viewDidLoad()
testKVO()
tc.ttlkvo = 30
secondkvc()
testTimer()
}
// KVO
func testKVO(){
tc.ttlkvo = 15
tc.addObserver(self, forKeyPath:"ttlkvo", options: NSKeyValueObservingOptions.New|NSKeyValueObservingOptions.Old, context: &mycontext)
}
deinit{
tc.removeObserver(self,forKeyPath:"ttlkvo")
}
override func observeValueForKeyPath(keyPath:String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if(context == &mycontext){
println("\(change)")
}
}
// KVC
class TestForKVC:NSObject{
var ttlkvc = "hello world"
dynamic var ttlkvo:Float =10
}
func secondkvc(){
var instance = TestForKVC()
instance.setValue("hello xxx",forKey:"ttlkvc")
var value = instance.valueForKey("ttlkvc")as String
println("\(value)")
}
// Timer
func testTimer(){
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: "timeaction", userInfo:nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
}
func timeaction(){
println("HHHYYY")
}
- 2常用的KVO、KVC、Timer
- KVO & KVC 的比较 - KVO
- KVO/KVC的理解
- KVO/KVC的理解
- KVC、KVO的用法
- KVO/KVC的demo
- KVC/KVO的理解
- KVO、KVC的探究
- KVC KVO的理解
- KVO/KVC简单的介绍
- KVC与KVO的不同
- KVC与KVO的用处
- KVC与KVO的不同
- KVC与KVO的不同
- ios KVC KVO的使用
- KVC和KVO的学习
- KVC和KVO的理解
- oc的kvc和kvo
- MYSQL的操作代码,备份、删除、数据库用法
- java中用到的几个write和read函数参数
- NSDateFormatter使用
- 黑马程序员---从头开始,回忆JAVA基础之正则表达式。
- app和WebView的交互
- 2常用的KVO、KVC、Timer
- FancyBox的使用技巧 (汇总)
- 产品设计与研发管理.一
- 关于iOS中UIView类视图的圆角
- 灰度图像--图像分割 Robert算子
- DebugGap调试腾讯X5内核webview
- 数据挖掘过程中:数据预处理
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 和 system.exit( )区别
- 如何编写高效android代码