Formatter Hides String Value
Formatter Hides String Value
- Subject: Formatter Hides String Value
- From: Daniel Todd Currie <email@hidden>
- Date: Mon, 26 Jul 2004 21:53:32 -0700
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.