检测UILabel文本中的更改 [英] Detect Change in UILabel Text
本文介绍了检测UILabel文本中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在UILabel的text属性更改时设置通知?当我找不到用于UILabel的UITextField时,我尝试了一个用于UITextFields的方法,但是它不起作用.
Is it possible to set a Notification for when a UILabel's text property is changed? I tried the one used for UITextFields when I couldn't find one for a UILabel, but it didn't work.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(posttosocial)
name:UITextFieldTextDidChangeNotification
object:nowplaying];
推荐答案
您可以使用键值观察(KVO):
You can use key-value observing (KVO):
[label addObserver:self
forKeyPath:@"text"
options:NSKeyValueObservingOptionNew
| NSKeyValueObservingOptionOld
context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"text"]) {
/* etc. */
}
}
这篇关于检测UILabel文本中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文