Re: NSNumberFormattter not working
Re: NSNumberFormattter not working
- Subject: Re: NSNumberFormattter not working
- From: Ken Tozier <email@hidden>
- Date: Thu, 12 Jul 2007 15:41:02 -0400
Well I think the format is basically written now. Only problem is
that the "isPartialStringValue " method never gets called. I can
still type in any character on the keyboard. Is there some additional
step needed to get this working? (beyond setting the text field
formatter to my custom formatter)
Here's the complete formatter code
#import "KIntegerFormatter.h"
static NSCharacterSet *gNotDigitCharSet = nil;
@implementation KIntegerFormatter
+ (void) initialize
{
gNotDigitCharSet = [[[NSCharacterSet decimalDigitCharacterSet]
invertedSet] retain];
}
- (NSString *) stringForObjectValue:(id) inObject
{
NSLog(@"entered KIntegerFormatter:stringForObjectValue");
if (![inObject isKindOfClass: [NSNumber class]])
return nil;
return [inObject stringValue];
}
- (BOOL) getObjectValue:(id *) outObj
forString:(NSString *) inString
errorDescription:(NSString **) outError
{
NSLog(@"entered KIntegerFormatter:getObjectValue");
int intResult;
NSScanner *scanner;
BOOL exeOK = NO;
scanner = [NSScanner scannerWithString: inString];
if ([scanner scanInt: &intResult] && ([scanner isAtEnd]))
{
exeOK = YES;
if (outObj)
*outObj = [NSNumber numberWithInt: intResult];
}
else
{
if (outError)
*outError = NSLocalizedString(@"Could not convert to
integer", @"Error converting");
}
return exeOK;
}
- (NSAttributedString *) attributedStringForObjectValue:(id) inObject
withDefaultAttributes:(NSDictionary *) inAttributes
{
// This method never gets called
NSString *val = [self stringForObjectValue: inObject];
return [[[NSAttributedString alloc] initWithString: val] autorelease];
}
- (BOOL) isPartialStringValue:(NSString *) partialString
newEditingString:(NSString **) newString
errorDescription:(NSString **) error
{
// This method never gets called
NSLog(@"entered KIntegerFormatter:isPartialStringValid");
NSRange range;
range = [partialString rangeOfCharacterFromSet: gNotDigitCharSet];
if (range.location == NSNotFound)
return YES;
*error = NSLocalizedString (@"You must enter a number", @"You must
enter a number");
*newString = nil;
return NO;
}
@end
And here's the complete integer field code
@implementation KIntegerField
+ (void) initialize
{
gIntFormatter = [[KIntegerFormatter alloc] init];
}
- (id) initWithFrame:(NSRect) inFrame
{
self = [super initWithFrame: inFrame];
if (self)
{
[self setFormatter: gIntFormatter];
[self setAction: @selector(handleTextEntry)];
[self setTarget: self];
}
return self;
}
// used for key/value change trigger
- (void) setIntegerValue:(int) inInt
{
val = inInt;
NSLog(@"val: %i", val);
}
// executes when user hits return char
- (void) handleTextEntry
{
NSLog(@"stringValue: %@", [self stringValue]);
[self setIntegerValue: [self intValue]];
}
@end
_______________________________________________
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