Hello all,

The quick summary:
I want to display some formatted numbers in text fields. These numbers are stored in NSUserDefaults, and need to be displayed with unit suffixes, eg: "5 cm". If the user enters an invalid number, say a negative length, I simply want to clip the value to the applicable minimum/maximum.

What I've tried:
I bound my text field's value to the shared NSUserDefaults controller. So far so good- the number is taken from the defaults and edits are stored back into the defaults.

I then attached an NSNumberFormatter to the text field. Nothing fancy, I just added a suffix of " cm" and set the minimum to zero. The problem is that entering a negative number throws up a sheet with the ever-so-helpful message "Formatting error." I don't want to bother the user with an error sheet.

Some internet searches led me to believe that when one uses bindings, one must also take care of the validation in the model object. That makes a lot of sense and seems like good coding practice anyways. So, happily I tried implementing a validator method via a category in NSUserDefaults, eg:

        - (BOOL) validateMySpecialValue:(id*)valuePtr error:(NSError**)errPtr

The problem is that this method is never called. I thought perhaps there's some special magic in NSUserDefaults (or I misspelled a key) that was interfering, so for testing I constructed a dead simple object with a single property, implemented using a hand-coded getter, setter, and validator. The getter and setter were called, but again not the validator. So I detached the formatter from my text field and Et Violá! My object's validator is now called.

My Questions:
Why is my validator method not called when the NSNumberFormatter is attached to the NSTextField? Is there some way around this? Am I missing something?

I should also say, even once I got my dead simple object validator method working (by not using a formatter), I still could not get the NSUserDefaults validator working. The method was simply never called, even when no formatter was involved. That's the ultimate goal here: formatting and validating values stored in the defaults.

Any help at all would be appreciated- perhaps I'm just going about things entirely the wrong way.

Thanks again,
~Martin

_______________________________________________

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

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

Reply via email to