0

I've got a textfield with number formatter which is bound to a variable with Cocoa bindings. There's also a stepper which I've bound to the same variable to increment the value, I've used an NSLog to test and it works great.

My problem is: Whenever I type a number into the box the variable isn't updated unless i hit enter. How does NSTextBox know when the user is finished entering? Is there a way to dynamically update the variable after every digit entered?

Many thanks

Rich

RichPorter
  • 87
  • 8
  • possible duplicate of [NSTextField continuous update](http://stackoverflow.com/questions/8410802/nstextfield-continuous-update) – Daij-Djan Jan 29 '13 at 16:09

2 Answers2

0

Switching on the "Continuous" property for the control in Interface Builder should make the field fire its binding as the value is changed.

iKenndac
  • 18,730
  • 3
  • 35
  • 51
  • Hi thanks for your response. That's what I thought, but It doesn't seem to be working. I'm using the int entered to drive a loop but the loop runs four times regardless of entry. 4 is the number thats issued to the variable when its created. – RichPorter Jan 29 '13 at 17:00
0

First, are you sure you want it to update immediately? One of the user's keystrokes could be an invalid character (ie, not a number). An NSNumberFormatter can help but I believe you still have to "commit" the entry before it will validate it as a proper number entry.

Second, you could always implement the -textDidChange: NSTextDelegate method. Again, same problem as above, though. Invalid input could trigger. To work around this, you could try creating an NSNumber from the string input and if you don't get an NSNumber, don't bother reacting until you do (and if the field loses focus with invalid input, change it back to its last valid value) but then you're duplicating the "validate on commit" NSNumberFormatter behavior you get for free.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • I'm happy for it to update immediately, as the loop wouldn't be run until the user pushed a button, so its unlikely to cause problems. I'd hoped the number formatter would help keep the input as expected. I think that the -textDidChange: method may be worth looking into, but that's going to involve a lot more learning for me. – RichPorter Jan 29 '13 at 17:30
  • Step 1: Implement -textDidChange:. Step 2: Set an instance of the class in which -textDidChange: is implemented as the text field's delegate. Done. :-) – Joshua Nozzi Jan 29 '13 at 17:51
  • Many thanks. I've got it working. Your last comment really helped. Implementing -textDidChange wasn't so hard. But I wouldn't have set the delegate if you hadn't mentioned it. – RichPorter Jan 29 '13 at 17:56
  • Happy to help. At least I accomplished something useful today. :-D – Joshua Nozzi Jan 29 '13 at 18:19