Detect UILabel text change in swift

前端 未结 5 914
情书的邮戳
情书的邮戳 2020-12-25 15:28

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 userInteractionE

5条回答
  •  误落风尘
    2020-12-25 15:41

    You can do this simply with a subclass of UILabel:

    class Label : UILabel {
        override var text: String? {
            didSet {
                print("Text changed from \(oldValue) to \(text)")
            }
        }
    }
    

    oldValue is a special value provided by Swift. See Property Observers

提交回复
热议问题