Why does controlTextDidChange(_ obj: Notification) not work for label ?. I want a function to be called when the stringValue of the label changes [macOS]
-
1This is MacOS , By definition Label cannot be edited or selected , please refer documentation before commenting – Prithvi Venu Jun 20 '19 at 10:52
-
1No bro pls be updated , in swift 5 they have created a separate constructor for label NSTextField(labelWithString: "").Of course yeah it is a just a text field , But a textfield is called as a label only when it can't be edited or selected – Prithvi Venu Jun 20 '19 at 14:25
-
1No, I have clearly mentioned textfield label, why do you when confused when I have clearly mentioned that i'm creating a textfield label.Please update yourself before pointing out mistakes – Prithvi Venu Jun 21 '19 at 03:56
1 Answers
controlTextDidChange
(and controlTextDidBeginEditing
and controlTextDidEndEditing
) are delegate methods that report user initiated changes to an editable text field. A label is not an editable, and therefor will never send these messages.
If you want a notification of a programatic label change, I would suggest using a binding. (I'm pretty sure changing the label value will fire a value changed notification. I've never used it this way, so you'll have to verify that.)
It would probably by easier (and I do this a lot) to simply bind the text value of the control to the string property of an object. You would then set the label by changing that property. Other code that needs to be notified of this change can observe that property, or wrap the update logic inside the didSet
property observer.

- 3,299
- 3
- 14
- 30