views:

69

answers:

1

Whenever a user would type a number, my app would automatically prepend a currency sign before that number. For example, when the user types "1" in a text field, the text inside it becomes "$1.00". All is good when I use an NSNumberFormatter, an NSTextField, and its delegate method control:didFailToFormatString:errorDescription:.

- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error
{    

    if ([[control formatter] isKindOfClass:[NSNumberFormatter class]])
    {
        NSNumberFormatter *formatter = [control formatter];

        if ([formatter numberStyle] == NSNumberFormatterCurrencyStyle && !
            [string hasPrefix:[formatter currencySymbol]])
        {

            NSDecimalNumber *new = [NSDecimalNumber decimalNumberWithString:string];
            if (new == [NSDecimalNumber notANumber]) {
                new = [NSDecimalNumber zero];
            }
            [control setObjectValue:new];
        }
    }

    return YES;}

Now I would like to have this functionality when a user types a number in a cell inside an NSTableView. I tried using control:didFailToFormatString:errorDescription: but the cell would erase the text instead.

A: 

I just want to let everybody know that I have solved the problem by asking for the NSTableView's currentEditor, and replacing the text from there.

Additionally, [control formatter] will return nil if the receiver is an NSTableView.

earl.ct