Number Formatter Default Value (or combo box?)
Number Formatter Default Value (or combo box?)
- Subject: Number Formatter Default Value (or combo box?)
- From: Seth Willits <email@hidden>
- Date: Fri, 15 Mar 2013 18:07:07 -0700
There's a possibility I just can't see what's in front of me. I have a combo box where the pulldown has some possible values (2, 4, 8), but the user should be able to enter anything from 1-8, so I've added a number formatter with a min and max value etc. The user, though can delete the text entirely which translates to a nil value which is apparently perfectly acceptable despite the min and max — the user sees no error, unlike if they entered 99 or 0.
Since the combo box value is bound to NSUserDefaults, it's very important that the value sent off to the binding *must always* be a valid value. So I'm thinking that either NSNumberFormatter or NSComboBox should perhaps have a "default value".
So I started fiddling and created a number formatter subclass and did:
- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string range:(inout NSRange *)rangep error:(out NSError **)error;
{
BOOL result = [super getObjectValue:obj forString:string range:rangep error:error];
if (!*obj) {
if (string.length == 0) {
*obj = @(4);
result = YES;
} else {
NSNumber * num = nil;
if (self.allowsFloats) {
num = @(string.doubleValue);
} else {
num = @(string.integerValue);
}
if (self.minimum && [num isLessThan:self.minimum]) {
*obj = self.minimum;
result = YES;
} else if (self.maximum && [num isGreaterThan:self.maximum]) {
*obj = self.maximum;
result = YES;
}
}
}
return result;
}
The behavior resulting from this is nice. It always produces a valid value (which is then shown in the field), and if the user enters and invalid value, they never see an error message, it just shows them a valid value — either my default (4) or the min or max value of the formatter if they entered an out-of-bounds value.
So my questions:
1) I see no way for a combo box to have a "default value" if say the formatter returns nil
2) I see no way for a formatter to have a "default value" if it can't convert a value.
Am I wrong on that? I don't think I am, so I guess I'll keep on trucking with the above code.
--
Seth Willits
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden