Building a calculator text field: problem with NSFormatter
Building a calculator text field: problem with NSFormatter
- Subject: Building a calculator text field: problem with NSFormatter
- From: Niko Matsakis <email@hidden>
- Date: Thu, 30 Nov 2006 06:50:04 +0100
Summary:
I have a problem that my custom NSFormatter is invoked on every key
press, and I'd prefer for it to be invoked only once the user tries to
change focus or otherwise "finishes".
Detailed explanation:
I have an NSTextField that is bound to an NSDecimalNumber field of
another object. Right now, the user can enter values into the field and
everything works fine. I was thinking it would be cool if the user
could enter an expression as well. For example, instead of entering
"22", they might enter "44/2" or "20+2".
The application is a financial one for tracking my daily budget, and
this is modeled after Quicken, which has a similar feature. In
Quicken's case, pressing an operator causes a small "calculation" window
to pop up ---- I figured I could do something simpler.
In any case, I tried to use an NSFormatter subclass, let's call it
ExpressionEvaluationFormatter, for this purpose: when given a string,
the formatter passes the string through an expression evaluation
function that I wrote which returns the resulting value, or throws an
exception if the value is not well formed.
The problem is that ExpressionEvaluationFormatter gets invoked each time
the user presses a key! This results in it being given invalid
expressions like "20+", rather than waiting until the user is finished.
I tried to overload the isPartialStringValid:proposedSelectedRange:(etc)
method, but I find that if this method returns YES, indicating that the
partial string IS valid, then getObjectValue:forString: is immediately
invoked, leading me back to the same problem. If the method returns NO,
on the other hand, then the invalid character is rejected, and so the
user cannot enter any binary operators. This seems to happen no matter
what "corrected" version of the string I return.
Plea for help:
So, any ideas how to make the text field wait until the user is finished
to submit its value to the NSFormatter? Or, perhaps there is another
approach that would work better --- subclassing NSTextField, a
delegate? Perhaps there is already a "NSCalculatingTextField" and I
just missed it?
thanks!
Niko Matsakis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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