Re: NSNumberFormattter not working
Re: NSNumberFormattter not working
- Subject: Re: NSNumberFormattter not working
- From: Ken Tozier <email@hidden>
- Date: Thu, 12 Jul 2007 13:58:43 -0400
Thanks for the tip Keary
I dove into NSFormatter and made this partial attempt but I must be
doing something wrong as the result was an absurd amount of code.
Here's what I think should happen, as pseudo code, in response to key
presses (if NSTextField allowed access to individual keypresses)
- (void) keyDown:(NSEvent *) inEvent
{
unichar aChar = [[inEvent charactersIgnoringModifiers] charAtIndex:
0];
if ((aChar >= '0') && (aChar <= '9'))
/* accept the character */
else
/* reject the character */
}
Simple clean, effective. And here's the integer franken-formatter so
far. (please tell me this isn't "the right" way to do this)
static NSCharacterSet *gDigitCharSet = nil;
@implementation KIntegerFormatter
+ (void) initialize
{
NSLog(@"entered KIntegerFormatter initialize");
gDigitCharSet = [[NSCharacterSet decimalDigitCharacterSet] retain];
}
- (NSString *) stringForObjectValue:(id) inObject
{
if (![inObject isKindOfClass: [NSNumber class]])
return nil;
return [inObject stringValue];
}
- (BOOL) getObjectValue:(id *) inObj
forString:(NSString *) inString
errorDescription:(NSString **) inError
{
int resInt = [inString intValue];
*obj = [NSNumber numberWithString:
}
- (BOOL) getObjectValue:(id *) outObj
forString:(NSString *) inString
errorDescription:(NSString **) outError
{
int intResult;
NSScanner *scanner;
BOOL exeOK = NO;
scanner = [NSScanner scannerWithString: string];
// Huh? What the heck is this for?
[scanner scanString: @"$" intoString: NULL]; //ignore return
value
if ([scanner scanInt: &intResult] && ([scanner isAtEnd]))
{
exeOK = YES;
if (obj)
*obj = [NSNumber numberWithInt: intResult];
}
else
{
if (error)
*error = NSLocalizedString(@"Couldn’t convert to
float", @"Error converting");
}
return exeOK;
}
- (BOOL) isPartialStringValid:(NSString *) partialString
newEditingString:(NSString **) newString
errorDescription:(NSString **) error
{
NSLog(@"entered KIntegerFormatter:isPartialStringValid");
if ([partialString length] == 0)
return YES;
else
{
int lastCharIndex = [partialString length] - 1;
unichar lastChar = [partialString characterAtIndex: lastCharIndex];
if ([gDigitCharSet characterIsMember: lastChar])
{
return YES;
}
else
{
*newString = nil;
return NO;
}
}
}
And here's where it is used in a custom NSTextField subclass (so far)
KIntegerFormatter *gIntFormatter = nil;
@implementation KIntegerField
+ (void) initialize
{
gIntFormatter = [[KIntegerFormatter alloc] init];
NSLog(@"gIntFormatter: %@", gIntFormatter);
}
- (id) initWithFrame:(NSRect) inFrame
{
self = [super initWithFrame: inFrame];
if (self)
{
[self setFormatter: gIntFormatter];
[self setAction: @selector(handleTextEntry)];
[self setTarget: self];
}
return self;
}
- (void) setIntegerValue:(int) inInt
{
val = inInt;
NSLog(@"val: %i", val);
}
- (void) handleTextEntry
{
NSLog(@"stringValue: %@", [self stringValue]);
[self setIntegerValue: [self intValue]];
}
@end
On Jul 12, 2007, at 11:48 AM, Keary Suska wrote:
on 7/12/07 8:59 AM, email@hidden purportedly said:
Well I played around with a formatter in IB and apparently number
formatters don't prevent alpha entry they only beep on completion to
indicate that something is wrong.
I must be "fighting the framework" here, as I can't believe that
something so conceptually simple as restricting text input to only
valid numeric characters, would require delegates or overriding the
application's key handling mechanism. (Which is what several threads
at CocoaDev suggested)
I know the following NSTextField method doesn't exist, but is there
some reasonably simple technique that would simulate it's
functionality?
[aTextField setValidCharacterSet: [NSCharacterSet
decimalDigitCharacterSet]];
All you need is a custom NSFormatter subclass. Read the NSFormatter
API,
particularly -isPartialStringValid:newEditingString:errorDescription:
Best,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40comcast.net
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden