Search code examples
xcodecocoaeventsnstextfieldibaction

NSTextField call IBAction on text change


In visual studio there is an event called text_changed for text boxes, how can I create a similar event/Action in Cocoa?

And in general how do you get these extra events for form controls? e.g. mouse over, in visual studio you just choose from a list of events.


Solution

  • You want to set yourself (i.e. the app delegate, or whatever controller you've got for managing that view) as the delegate of your NSTextField. Then implement the method

    - (void)controlTextDidChange:(NSNotification *)aNotification
    

    It gets called whenever the text in the text field changes.

    For an introduction to handling mouse events, such as mouse over, the Cocoa Event Handling Guide is a pretty good starting point.