4

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.

Charlie Egan
  • 4,878
  • 6
  • 33
  • 48
  • Normally you have to press enter/tab/leave focus to get the value to update. I want it so when the user types it updates on every character change. – Charlie Egan Jul 09 '12 at 20:12

1 Answers1

5

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.

puzzle
  • 6,071
  • 1
  • 25
  • 30
  • i did have a previous answer but the guy must have removed it, i got that far but i don't know how i can make this event submit the value, much like when the user normally presses tab or enter, but the control keeps the focus. – Charlie Egan Jul 10 '12 at 08:16
  • What do you mean by "submit"? If you set yourself as the delegate of the text field then the above method will get called every time the user types something into the text field. You can simply retrieve the text field's `stringValue` from that method and do whatever you need to do with it. – puzzle Jul 10 '12 at 22:19
  • It's maybe hard to explain, you know when you bind a textfield to a label and set it to update continuously? well i want it to be like that, but with out the binding. But the value i want to update is in another class, how do i update that value in the other class? – Charlie Egan Jul 11 '12 at 07:41
  • You implement the delegate method somewhere suitable, set that object to be the text field's delegate, and then from your delegate method, set the value in the other object. – puzzle Jul 12 '12 at 21:26
  • okay, now i just need to work out how to change the value in the other object. But i should manage that with just me and google. Thanks again. – Charlie Egan Jul 13 '12 at 08:14