简体   繁体   English

iOS如何查明UILabel是否更改了它的文本

[英]iOS how to find out if a UILabel has changed it's text

Just had a question regarding the UILabel class. 刚刚有一个关于UILabel类的问题。 I know that the UITextField control has a delegate for when the editing has changed, but I was wondering if the UILabel has a similar field. 我知道UITextField控件有编辑更改时的委托,但我想知道UILabel是否有类似的字段。 The reason I ask is that my application queries the network for when certain pieces of information change and updates them accordingly and I don't want to hard code in my update the name of the label I want to watch for just to do a small task regarding it. 我问的原因是我的应用程序在网络中查询某些信息的变化并相应地更新它们并且我不想在我的更新中硬编码我想要观察的标签名称只是为了完成一项小任务关于它。 If it has a delegate or something equivalent to it so that my class can monitor for this information that would be great. 如果它有一个委托或类似的东西,以便我的班级可以监控这些信息,这将是伟大的。

If not then any advice would be greatly appreciated. 如果没有,那么任何建议将不胜感激。

You can use KVO to find out or perform some code when the text changes like this : 当文本更改时,您可以使用KVO查找或执行一些代码:

[somelabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:NULL];

and then listen for the changes like this 然后听这样的变化

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    NSLog(@"the text changed");
}

A UILabel cannot change its text without your code doing it, so why would there ever be a delegate that told you when it's changed? UILabel无法在没有代码的情况下更改其文本,那么为什么会有一个代表在更改时告诉您? You already know... UITextField has one because the user can edit the text so you're told when it's changed. 你已经知道了... UITextField有一个,因为用户可以编辑文本,这样你就会被告知它何时被改变了。

So basically, no, there is no delegate method and you shouldn't even need one. 所以基本上,不,没有委托方法,你甚至不需要一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM