是否可以为 UILabel 的文本属性更改时设置通知?当我找不到用于 UILabel 的那个时,我尝试了用于 UITextFields 的那个,但它没有用。
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(posttosocial)
name:UITextFieldTextDidChangeNotification
object:nowplaying];
最佳答案
您可以使用键值观察 (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. */
}
}
关于ios - 检测 UILabel 文本的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14326566/