Re: Formatter Hides String Value
Re: Formatter Hides String Value
- Subject: Re: Formatter Hides String Value
- From: Daniel Todd Currie <email@hidden>
- Date: Tue, 27 Jul 2004 11:16:09 -0700
Ok, so I found the problem... It occurs when my formatter is a
subclass of NSNumberFormatter, but it works fine when it is a subclass
of NSFormatter. Is there a method I need to be overriding or something
to make my NSNumberFormatter subclass work? Or am I even allowed to
subclass NSNumberFormatter? Is this perhaps a bug?
If someone could try changing one of their custom formatters to a
subclass of NSNumberFormatter, we could check for reproducability.
-- DTC
On 2004 Jul 26, at 21:53, Daniel Todd Currie wrote:
I have an NSTextField that is attached to a custom NSNumberFormatter
subclass (code below). Whenever this text field is first responder,
the formatter works perfectly. However as soon as another object
becomes first responder, the contents of my text field disappear.
- The text field works normally when i disconnect it from my formatter
subclass in IB.
- I have checked [myTextField stringValue] while the field is not
first responder and the string is still intact.
- When the text field becomes first responder again, the string value
becomes visible.
- I have attached the formatter subclass to other text fields in other
apps, and have had the same problem.
How can the formatter control text visibility in my text field?
Thanks in advance,
-- DTC
======== PTAmplitudeFormatter.h ========
#import <Cocoa/Cocoa.h>
@interface PTAmplitudeFormatter : NSNumberFormatter
@end
======== PTAmplitudeFormatter.m ========
#import "PTAmplitudeFormatter.h"
@implementation PTAmplitudeFormatter
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
{
*obj = string;
return(YES);
}
- (NSString *)stringForObjectValue:(id)anObject
{
if([anObject isKindOfClass:[NSString class]])
{
return(anObject);
}
else
{
return(nil);
}
}
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString
**)error
{
NSRange foundRange;
// allow numbers, decimal point
NSCharacterSet *disallowedCharacters = [[NSCharacterSet
characterSetWithCharactersInString:@"0123456789."] invertedSet];
foundRange = [partialString
rangeOfCharacterFromSet:disallowedCharacters];
if(foundRange.location != NSNotFound)
{
*error = @"Amplitude input contains one or more non-numeric
characters.";
NSBeep();
return(NO);
}
// limit input to < 1
if([partialString doubleValue] > 1)
{
*error = @"Amplitude input is greater than 1.";
NSBeep();
return(NO);
}
// disallow more than one period
UInt8 i;
BOOL firstPeriod = NO;
for(i = 0; i < [partialString length]; ++i)
{
if([[partialString substringWithRange:NSMakeRange(i, 1)]
isEqualTo:@"."])
{
if(firstPeriod)
{
*error = @"Multiple periods are not allowed.";
NSBeep();
return(NO);
}
firstPeriod = YES;
}
}
*newString = partialString;
return(YES);
}
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.