I’m trying to understand how to catch a “text changed” event from a text field in my window. I’m used to Java’s “action listeners”, and can’t find anything similar in Objective-C/Cocoa.
I searched for quite a while and found the “key value observing” protocol, but the observeValueForKeyPath: method (function?) only triggers when the value of my text field was changed in code (using [textfield setStringValue:...], e.g.), not by typing in it.
How can I “listen” to the value change when a user types in the text field?
You can set a delegate for your
NSTextFieldinstance and have the delegate implement the following method:Your delegate object can be the application delegate, a window controller, a view controller, or some other object in your application. The delegate can be programatically set via
or, in Interface Builder, via the
delegateoutlet available in theNSTextFieldcontrol.Note that if there are multiple controls hooked to the same delegate then
-controlTextDidChange:will be sent for each control, i.e., the same method is called for different controls. If you want different behaviour according to the control where the text has changed, you can use-[NSNotification object]to identify the control that has sent the notification.For instance, if you have two text fields with corresponding outlets
nameFieldandaddressField, and you’ve set the same delegate for both fields, then:Alternatively, you could have one delegate for each text field. In this case, there’d be no need to test
[notification object].