问题描述
是否有一种方法可以在UILabel
的文本发生更改时得到通知,或者最好将userInteractionEnabled
设置为false
的UITextField
并使用其UIControlEditingChanged
事件来获得通知达到我的目的?
Is there a way to get notified when there is a change in a UILabel
's text or would I be better off using a UITextField
with userInteractionEnabled
set to false
and using its UIControlEditingChanged
event to fulfil my purpose?
例如每次更改UILabel
的文本时,我都需要运行某些代码行,并相应地进行更改.因此,我不想在每次更改UILabel
的文本时都写这100行几乎相似的代码,而是希望将其一起写在一个地方,并在每次UILabel
更改时调用它.我什至不知道这是否有意义.原谅我,但我不能透露很多代码.
For ex. I need to run certain lines of code every time I change the UILabel
's text and accordingly. So instead of writing those 100 lines of almost similar code for every case I change the UILabel
's text, I wish to write it together in one place and call it every time the UILabel
is changed. I don't even know if that makes any sense. Forgive me but I cannot expose much of the code.
推荐答案
创建一个从UILabel继承的类.如:
Create a class that inherits from UILabel. Such as:
class YourLabel: UILabel {
override var text: String? {
didSet {
if let text = text {
println("Text changed.")
} else {
println("Text not changed.")
}
}
}
}
为您的对象创建此类的出口.
Create a outlet of this class for your object.
@IBOutlet weak var label: YourLabel!
label.text = "abc"
这篇关于快速检测UILabel文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!