|
|
NSFormatter question
|
|
|
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
I have a textfield in which i only allow the user to type a single letter. I need to format this single letter after its typed into uppercase. so if the user types "t" what actually gets put into the textField is "T". Ive subclassed NSFormatter and it sort of works but not really. When I type "t" it doesnt change appear as "T" UNLESS I hit tab. I discovered this accidently. When i hit tab te focus doesnt change to anywhere else, my t just turns into T. Heres my formatter code.
any ideas?
- (id)init {
if (self = [super init]) {
crappyCharacterSet = [[[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklm nopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet] retain];
return self;
}
return nil;
}
- (NSString *)stringForObjectValue:(id)anObject {
return [anObject uppercaseString];
}
- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error {
if (error) {
*error = @"error";
}
*anObject = string;
return YES;
}
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error {
if (([partialString containsCharacterInSet:crappyCharacterSet]) || ([partialString length] > 1)) {
NSBeep();
return NO;
}
*newString = partialString;
return YES;
}
- (void)dealloc {
[crappyCharacterSet release];
[super dealloc];
}
@end
|
3R1C
|
|
|
|
|
|
|
|
Senior User
Join Date: Feb 2003
Location: USA
Status:
Offline
|
|
I don't know about NSFormatter really, but in one of my apps I had the need to do something as soon as a user typed in a text field, I used a notification center and :
- (void)controlTextDidChange:(NSNotification *)aNotification
Sorry if that's no help...
|
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
|
|
|
|
|
|
|
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
To have the field replace the string as it's typed, you need to pass back the corrected string from the partial string validation method. So, instead of:
Code:
*newString = partialString;
you should have
Code:
*newString = [partialString uppercaseString];
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
That was part of it. I also needed to return No instead of Yes.
|
3R1C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|