KVO监听对象的属性值改变。不能用于输入框的实时输入监测。
#import <Foundation/Foundation.h>
//为某个对象的某个属性添kvo监听
[self.remarkTextView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
//移除监听
- (void)dealloc{
[self.remarkTextView removeObserver:self forKeyPath:@"text"];//移除监听
}
//监听值变化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if([keyPath isEqualToString:@"text"]){//这里只处理text属性
if(![[change objectForKey:@"new"]isEqualToString:[change objectForKey:@"old"]]) {
if (self.remarkTextView.text.length == 0) {
self.placeHolderLabel.text = @"请输入您的备注信息";
}else{
self.placeHolderLabel.text = @"";
}
}
}
}
网友评论