NSFormatter/NSControl and valid object notification
NSFormatter/NSControl and valid object notification
- Subject: NSFormatter/NSControl and valid object notification
- From: Jason Stephenson <email@hidden>
- Date: Sun, 22 Jun 2008 18:14:50 -0400
I am writing a Cocoa application as a GUI for a command line utility,
and I decided to create a custom NSFormatter to ensure that the value
entered in a NSTextField matches what the command line program expects
for that parameter. (The format is actually rather complicated since it
will accept domain names, dotted quads in CIDR notations, IP6 addresses,
and address ranges that can be expressed with - and , in any of the
quads, and a couple of extra special cases.)
So, I have the rudiments of the formatter working, but I haven't got far
enough that scrapping it would be a heartbreak.
My AppControllerinstantiates a TargetFormatter (the name of my formatter
object) in -awakeFromNib and then sets the instance as the formatter for
my text field's cell. I have also made my AppController object the
delegate for the text field, so that it handles the
-control:didFailToFormatString:errorDescription:. When I get that
message, I pop up an alert with the reason for the failure.
All is good so far, but now I want to make the run button's enabled
state cue off of the validity of the content of my text field. That is I
want the button disabled until there is valid content in the text field.
The main problem with this is that I don't want my formatter and my
button to have to know anything about each other.
Now, I see that using the NSControl delegate, I can easily find out in
AppController when the validation fails, but I don't see any messages
that are sent if the validation succeeds.
I see that NSControl can ask its delegate if the object value it
contains is valid or not, but implementing that would likely mean giving
up on my formatter, thought the formatter really seemed like the "right
thing" to do at the time (yesterday).
So, I guess at this point, my options are:
1. Adding a delegate to my NSFormatter that can be sent messages from
the -getObjectValue:forString:errorDescription: method in my formatter.
2. Posting some sort of notification from the above that my
AppController objects registers for.
3. Dropping the custom formatter and implementing the
-control:isValidObject: method in my AppController.
Then, AppController, which knows about the button can set the button's
state appropriately.
So, I am writing to the list to ask which path should I take and to find
out if maybe there is some other delegate method or notification that I
might have missed that the control or cell sends after calling
-getObjectValue:forString:errorDescription: on the formatter.
(Actually, I believe that the latter must be possible. I can't be the
only one who wants to control some other object's state based on the
validity of the object in a NSFormatter subclass. However, if it is not
possible, I won't be too surprised.)
Cheers,
Jason
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden