0

I have a NSTextField with a NSNumberFormatter. I set the formatter with a min of 0 (because I couldn't set it to 0.01), and the style to decimal. The NSTextField has a binding on its value with a float ivar, and the action is set to "Send On Enter Only". This works just fine.

What I'd like to do is if the user tries to erase the value and either clicks off, or presses enter, I want to restore the original value before editing.

I tried:

-(void) setNilValueForKey:(NSString*) key {
    if ([key compare:@"valX"] == NSOrderedSame) {
        self.valX = valX;
    }
}

But this doesn't set the NSTextField. I'm at a loss, any help is appreciated.

Thanks GW

GW.Rodriguez
  • 1,181
  • 8
  • 18

1 Answers1

0

For case of erasing, Implement the following delegate method as

- (void)controlTextDidChange:(NSNotification *)notification
{
    NSTextField* textfield = [notification object];

        if([textfield intValue] == 0)
        {
            [textfield setValue:valX];
        }
}

For case of pressing Enter and clicking off, implement

- (void)controlTextDidEndEditing:(NSNotification *)obj
{


    NSTextField* textfield = [notification object];
       [textfield setValue:valX];


}
PR Singh
  • 653
  • 5
  • 15
Neha
  • 1,751
  • 14
  • 36
  • This basically got me there. I had to override setNilValueForKey as well. But these delegate methods are called after the bindings are set. – GW.Rodriguez Oct 14 '13 at 21:46
  • yes you will have to set your class as the delegate of text field first – Neha Oct 15 '13 at 06:58