Re: NSNumberFormatter behavior with minimum/maximum values.
Re: NSNumberFormatter behavior with minimum/maximum values.
- Subject: Re: NSNumberFormatter behavior with minimum/maximum values.
- From: Matt Neuburg <email@hidden>
- Date: Fri, 10 Nov 2006 10:07:16 -0800
- Thread-topic: NSNumberFormatter behavior with minimum/maximum values.
On Thu, 9 Nov 2006 10:49:40 -0800, Tom Coates <email@hidden> said:
>I'm having difficulty understanding how to use an NSNumberFormatter
>in an NSTextField to only allow numbers within a certain range to be
>entered. I have a sheet-panel with the NSTextField. The Text field
>has the formatter "embedded" in it. The Panel also has an OK button
>which allows accepting the value. I don't want to let the user enter
>numbers outside of a range, say from 0 to 10. I've entered that
>range into the formatter settings.
>
>The behavior I get is that the NSTextField will let me input
>"illegal" values, but won't let me Tab out of the field unless I put
>in correct values. I get a Beep instead. I guess this means that
>formatter won't let the field resign its first responder status. But
>the OK Button still works and lets the user effectively enter the
>value. I'm not expecting that behavior by default. But I can't
>figure out how I'm supposed to disable the button based on the
>formatter's rejection of the bad value.
>
>So far I haven't been able to find any documentation on how the
>Minimum and Maximum controls should be used in an effective UI.
> Is there a tutorial somewhere that better explains how to use
>NSFormatter to limit input?
> Is it possible to wire up the behavior I need using Interface Builder?
> How does one detect that the NSNumberFormatter would reject the
>value shown?
> Is this a bindings sort of problem? I would think I should be able
>to get the OK Button to disable itself when the formatter has an
>"invalid data" property.
>
>This has to be a common problem. But maybe I'm expecting too much to
>be automatic. Any suggestions appreciated.
(1) When the button is pressed, the first thing your code should do is test
if(![[myTextField window] makeFirstResponder: nil]) ...
That will end editing, thus submitting the value to the formatter. If the
test fails, the button should not perform its normal action.
(2) In the text field's delegate, implement
control:didFailToFormatString:errorDescription:
You will be called instead of the beep, and now you can do whatever you
like. What you should like is (a) to figure out why the validation failed,
and (b) to put up an error. Use NSError and -[NSApp presentError:] to that
the error message gets propagated up the error responder chain coherently.
(3) For much more control, write your own formatter. The built-in
NSNumberFormatter is just to get you started; subclass it. Reading the docs
will help you. NSFormatter is quite clear on what should happen when
getObjectValue:forString:errorDescription: fails.
(4) Everything I've said here is straight out of cocoabuilder.com, so your
first move should be (or "have been") to check the archives.
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>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden