Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSFormatter question

NSFormatter question
Thread Tools
3R1C
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 2, 2004, 04:11 PM
 
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
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Sep 3, 2004, 08:54 AM
 
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
     
bewebste
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Sep 3, 2004, 04:54 PM
 
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];
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
Sep 11, 2004, 06:45 AM
 
That was part of it. I also needed to return No instead of Yes.
3R1C
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:26 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,