I’m writing a Mac OS X desktop Cocoa app. I’ve got an NSTableView with some values and I’m trying to control those values using a custom NSNumberFormatter.
My desired behavior:
As a user is typing a value, if they type -, I want it to behavior similar to a +/- button on a calculator. So if the user types “500-” then I want to automatically replace that string with “-500”.
Now, I’ve got a working subclass of NSNumberFormatter which handles this situation and returns my desired replacement string. But when I’m typing into my NSTableView, my logs are showing me that “500-” is getting replaced with “-500” but the NSTableView only shows “500” and doesn’t show the – either before OR after.
Is there something I need to do in my xib or in my NSNumberFormatter subclass so that the NSTableView will actually show the negative number?
- (BOOL) isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error
is logging this to the console:
proposed string: 500-
replacement string: -500
But the cell in the NSTableView still shows just “500” afterward.
It turns out that my old school C skills are just rusty and I wasn’t passing the new string back correctly.
I was incorrectly writing:
But the correct code is: