Re: User interface validation doesn't work, right?
Re: User interface validation doesn't work, right?
- Subject: Re: User interface validation doesn't work, right?
- From: Quincey Morris <email@hidden>
- Date: Fri, 3 Jul 2009 02:44:42 -0700
On Jul 3, 2009, at 01:20, Bill Cheeseman wrote:
In the case of buttons, you likewise have to implement -
validateUserInterfaceItem: yourself. But that isn't enough, because
Cocoa does not call it automatically just because you implemented
it. A generic solution is to implement the -windowDidUpdate:
delegate method in your window controller, and in it loop through
all the subviews in your window's contentView. In each iteration of
the loop, call your -validateUserInterfaceItem: method, testing
first to make sure the item implements -action and -tag and -
setEnabled: (or just test whether it's a button, since all buttons
do). If efficiency becomes a problem, take steps to reduce the
frequency with which -validatedUserInterfaceItem: is called (every
time the window updates -- that is, once every time through the run
loop -- may be overkill for many validation scenarios).
You should test for the specific view classes you want to have
validated (just NSButton, I think, but I may have missed something
from earlier in the thread). Don't try to initiate validation on every
item (even if it implements action, tag and setEnabled), because you
don't know for sure what other classes may already have validation
systems in place (like menus and toolbars do, though they of course
won't be found among the subviews).
I would also check that the button *doesn't* conform to
NSValidatedUserInterfaceItem, because if it does that certainly means
it is already participating in a validation system being initiated
elsewhere. There don't appear to be any such systems (apart from menus
and toolbars) in the Cocoa frameworks under Leopard, but who knows
what may change in the future?
Then the only danger is that if someone else (e.g. a 3rd-party
framework) implements a validation system for NSButtons in the same ad-
hoc way you're proposing, then your two systems will conflict.
However, if that's the case, you probably won't know of it (except
that validation will occur twice as many times), and there's not much
you can do about it, so it's not worth worrying about.
When you invoke validateUserInterfaceItem:, your window controller
also needs to make sure to send the message to the button's effective
target, not to self, similar to what's described in:
http://developer.apple.com/documentation/Cocoa/Conceptual/UIValidation/Articles/ValidatingObjects.html#/
/apple_ref/doc/uid/20000745
so the code will look a bit like this:
id validator = [NSApp targetForAction:[button action] to:[button
target] from: button];
if ((validator == nil) || ![validator respondsToSelector:[button
action]])
[button setEnabled:NO];
else if ([validator
respondsToSelector:@selector(validateUserInterfaceItem:)])
[button setEnabled:[validator validateUserInterfaceItem: (id<
NSValidatedUserInterfaceItem>) button]];
else
[button setEnabled:YES];
I agree that the frequency of the validation is a side issue, to be
dealt with if there is a problem. In general, there may also be a
problem with validation *not* getting called unless there is a window
update, if the validation result might change when there is no visible
change to the window. (I believe there's a flaw like that in the
toolbar automatic validation, but I've never been able to pin it down.)
_______________________________________________
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