0

I'm making a small program that has four NSTextField boxes. Each box is related to the others by a formula. Basically, I have it working right now where after you click out of the box, it'll calculate the other boxes just fine (you can enter numbers into any of the boxes).

What I want is the after each key-press, I'd like to call the boxes action to calculate the other boxes. So, the results should happen immediately, not only after clicking out of the box or hitting tab, etc..

I'm still learning ObjC, so, while I've tried to review the docs, they don't seem to apply directly to my issue.

I've defined the following actions:

- (IBAction)boxX1:(id)sender;
- (IBAction)boxY1:(id)sender;
- (IBAction)boxX2:(id)sender;
- (IBAction)boxY2:(id)sender;

For each one, there's some code that calls the calculate function in the right way...

So, the basic question is.. How do I watch keyDown: events, and specifically, know that the keyDown event happened in boxX1 vs. boxY2 and, be able to take the content of the boxes (after the keyDown event) and perform my calculation?

For example, here's the code for boxX2:

- (IBAction)boxY2:(id)sender {
    self.boxX2.stringValue = [ARCBrain calculateRatio:self.boxX1.stringValue times:self.boxY2.stringValue dividedBy:self.boxY1.stringValue];
}

I just need to be able to call it after each keypress.. Seems easy enough, no?

Thanks

DrDavid
  • 904
  • 3
  • 9
  • 18
  • You would have to subclass to get actual key down events, and you don't want those anyways because you'd have to interpret them into text yourself. You need to make a text field delegate and watch for the text change notifications. See: http://stackoverflow.com/questions/6164471/listen-to-a-value-change-of-my-text-field – jscs Apr 19 '12 at 17:32
  • Sorry, I'm still learning ObjC, so, finding it hard to grasp how to implement this.. I've added a bit more of the code I'm using above. Maybe that will make more sense? I've tried to implement the controlTextDidChange: but the method never gets called it seems.. – DrDavid Apr 19 '12 at 17:53
  • You have to make sure that the object that implements `controlTextDidChange:` is actually set as the delegate for the various text fields. Do that either in Interface Builder (the fields have a "delegate" outlet) or `awakeFromNib:`: `[boxX2 setDelegate:self];` – jscs Apr 19 '12 at 18:42

2 Answers2

2

Here's what I ended up doing:

I added this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:NSControlTextDidChangeNotification object:nil];

to the -applicationDidFinishLaunching: method and then, added:

-(void) textDidChange:(NSNotification *)obj {
   if ([obj object] == self.boxX1) {
      //do stuff
   } else ... {
   // Do other stuff
   }
   ...
}

to the class.. Worked like a charm!

DrDavid
  • 904
  • 3
  • 9
  • 18
-1

Check if you can use this method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 
Nilesh
  • 1,493
  • 18
  • 29