Restrict NSTextField input to numeric only? NSNumberformatter

MonkeyBlue picture MonkeyBlue · Jan 11, 2011 · Viewed 13k times · Source

I'm just getting started up with Mac App Development and so far everything is good well, I'm just having problems trying to get a NSTextField to only accept numbers for the input.

So far I have added a formatter to the NSTextField and set it to only allow decimal now when I enter letters in the field it still allows me but does not allow my to click out of the cell when done.

Ideally I would like the app not to let the user type in any letters and just beep or do nothing.

Any tips or pointers on this would be great

Answer

Mike Abdullah picture Mike Abdullah · Jan 11, 2011

Subclass NSNumberFormatter and implement this method:

- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
              originalString:(NSString *)origString
       originalSelectedRange:(NSRange)origSelRange
            errorDescription:(NSString **)error