Re: Invalid number in bound text field
Re: Invalid number in bound text field
- Subject: Re: Invalid number in bound text field
- From: Matt Neuburg <email@hidden>
- Date: Tue, 28 Nov 2006 09:31:10 -0800
- Thread-topic: Invalid number in bound text field
On Mon, 27 Nov 2006 18:14:57 -0700, "Tom Harrington" <email@hidden>
said:
>I have an NSTextField which should only contain floats. So it has an
>NSNumberFormatter. It's also bound to a float in the controller.
>
>If I enter something that's not a float (e.g. letters) and attempt to
>tab out of the field, I get a sheet that pops up warning me I have an
>invalid number. It's functional but crude in that all it says is
>"Invalid Number". Is there some way I can substitute my own message
>here, to say something a little more helpful?
>
>I thought that control:didFailToFormatString:errorDescription: might
>be what I wanted, but that just tells me the error that's being
>displayed for the user, without any option to change it
control:didFailToFormatString:errorDescription is useful in the unbound
case:
<http://www.cocoabuilder.com/archive/message/cocoa/2006/11/10/174237>
However, this is a bound case, which is different.
In the Date Assistant in NotLight, where a certain bound text field requires
a positive integer, I take the following approach.
The NSFormatter is not attached to the NSTextField at all - it is attached
to the NSController subclass. So, I implement
validateValue:forKeyPath:error: in the NSController subclass. This is called
automatically, provided the NSTextField "validates immediately". (How often
we are called depends upon "update continuously".) I then turn to the
NSFormatter and ask it for a verdict (e.g. with getObjectValue:...). If the
NSFormatter gives me a thumbs down, I use the information it provided, along
with any other logic I like, to create an appropriate NSError object to pass
along to the error: pointer. This gets passed up the responder chain and
presented as a dialog to the user.
One very nice benefit to this approach is that as part of the NSError I can
provide recovery. In this particular case what I do is put Revert and Cancel
buttons in the NSError. When the user presses one of the buttons in the
dialog, I get called back (via attemptRecoveryFromError:optionIndex:); if
the user pressed Revert, I call discardEditing. Thus we are taking advantage
of the fact that an NSController has built-in revert/commit for editing.
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
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