Replacing text in NSTextFieldCell inside NSTableView

Posted by earl.ct on Stack Overflow See other posts from Stack Overflow or by earl.ct
Published on 2010-05-25T16:21:37Z Indexed on 2010/05/25 17:11 UTC
Read the original article Hit count: 364

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa