NSFormatter troubles
NSFormatter troubles
- Subject: NSFormatter troubles
- From: Donald Hall <email@hidden>
- Date: Thu, 13 Jun 2002 00:28:20 -0600
I'm fairly new to Cocoa and having some problems with a custom formatter
that I hope someone can help me with.
I have a TableView with several columns. In one of the columns I want
the user to be able to type in the names of the days of the week, but
nothing else, so I made a custom formatter, following the example in
Hillegass Chapter 21. Here are the relevant methods:
// private method
-(NSString *)firstMatchForPartialString:(NSString *)string
{
NSRange whereFound;
NSString *day;
int i;
for (i = 0; i < [days count]; i++) {
day = [days objectAtIndex: i]; // days is an array containing
@"Monday", etc
whereFound = [day rangeOfString:string
options:NSCaseInsensitiveSearch];
if ((whereFound.location == 0) && (whereFound.length > 0)) {
return day;
}
}
return nil;
}
-(NSString *)stringForObjectValue:(id)obj
{
if ([obj isKindOfClass: [NSString class]]) {
return obj;
}
return nil;
}
-(BOOL)getObjectValue:(id *)obj
forString:(NSString *)string
errorDescription:(NSString **)errorString
{
// see if 'string' matches any of the names in the days array
NSString *match = [self firstMatchForPartialString:string];
if (match) {
NSLog(@"getObjectValue YES branch string=%@ match=%@", string,
match);
*obj = match;
return YES;
} else {
if (errorString != NULL) {
*errorString = @"No such day";
}
NSLog(@"getObjectValue NO branch string=%@ match=%@", string,
match);
return NO;
}
}
-(BOOL)isPartialStringValid:(NSString *)partial
newEditingString:(NSString **)newString
errorDescription:(NSString **)error
{
NSString *match;
if ([partial length] == 0) {
return YES;
}
match = [self firstMatchForPartialString:partial];
if (match) {
return YES;
} else {
if (error != NULL) {
*error = @"No such day";
}
NSBeep();
return NO;
}
}
I attached this formatter to the data cell of the table column in the
awakeFromNib method of my document class. If I attempt to type invalid
characters into a row in the column, I get the correct behavior (i.e.
isPartialStringValid:... works fine). However, if I initially load the
table with an invalid string from the data source, then double click to
select the invalid string, I am able to tab to the next column even
though getObjectValue:forString:errorDescription: returns NO (verified
by NSLog as above). Because there is no match made, the table cell then
displays nothing. Is there something else I have to do to prevent the
end of cell editing? I tried to implement the control delegate methods
control:didFailToFormatString:errorDescription and
control:textShouldEndEditing: by making my document object the delegate
of the table view, but neither is called. (I assume the table view is
the correct NSControl object whose delegate is sent the
control:didFailToFormatString:errorDescription message when the data
cell formatter decides the string is invalid?)
Sorry for the long post, but I am stumped.
Thanks for any help,
Don
----------------------------------------------
Donald S. Hall, Ph.D.
Apps & More Software Design, Inc.
http://www.theboss.net/appsmore
_______________________________________________
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.