On Aug 23, 2013, at 10:57 AM, Rob Nikander wrote:

> I want to constrain an NSTextField to float values greater than zero, so I
> connected an NSNumberFormatter in Interface Builder. Now the user can enter
> invalid text (eg, "1a", "-2") but it beeps when you try to leave the field.
> That may be okay.
> 
> Now I'd like to disable other parts of the UI when the field is invalid. I
> connected a delegate to the NSTextField, and implemented
> `controlTextDidChange:`, but I don't know how to check the validity from
> within that method.  I get some strange behavior when I call
> textField.doubleValue, or even textField.stringValue, from inside
> controlTextDidChange.  It causes the textfield to behave differently --
> instead of allowing invalid text and beeping later, it erases the invalid
> text as soon as you type it.
> 
> So first question: why is that happening? What is getting called by
> `textField.whateverValue` to change the field?
> 
> Second question: how to I ask the NSNumberFormatter about the validity of
> the field as the field changes?  I'd call its `getObjectValue...` method
> but I can seem to get the field's string value in `controlTextDidChange:`,
> without changing the behavior as described above.

Think MVC--your controller, likely the field's delegate, can both know whether 
the field value is valid and when the value changes, as you already do.

So, adding an outlet in the controller class to the formatter, as well as 
outlets to all the controls that you want to enable/disable, then in the 
controlTextDidChange delegate:

1. call formatter's getObjectValue:forString:range:error:
2. If it returns NO, disable the controls, and possibly zero the model value so 
you know it is invalid;
3. if it returns YES, enable the fields and update the model value.

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to