Re: Enable Buttons
Re: Enable Buttons
- Subject: Re: Enable Buttons
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 2 Jul 2005 10:03:43 +0200
From: "M. Uli Kusterer" <email@hidden>
At 20:33 Uhr +0200 01.07.2005, Gerriet M. Denkmann wrote:
I'd add a doFindNext: method to the First Responder in IB, and
connect the button to that. Then, add that method to your document
class. Cocoa will do the Right Thing(tm), sending the action to the
current document and disabling the button if none of the objects in
the responder chain respond to the specified action.
I tried this. Cocoa does indeed the Right Thing in sending the
action to the current document.
But it does not disable the butten when there is not document at all.
What am I doing wrong?
As far as I know, the approach described above only works with
toolbar items and menu items. To have it work for any others, I guess
you'd need to write some code of your own.
As a starting point, you could use something like:
http://www.zathras.de/angelweb/blog-validate-user-interface.htm
but extend it with a check for a responder that accepts this message.
But it would probably be rather inefficient.
The "correct" way and a more efficient one would probably be to add
a KVC-compliant property to your app delegate class that returns
[[NSDocumentController sharedDocumentController]
currentDocument] != nil
and bind that to the button's "enabled" property. But I haven't tried
that before, so I can't tell you where the pitfalls in any of this
might be. I don't even know whether IB lets you hook up bindings to
methods in user-defined objects.
I tried this.
Created a method:
currentDocument { return [[NSDocumentController
sharedDocumentController] currentDocument] }
And bound currentDocument to "enabled" via a non-nil transformer.
Worked perfectly.
The only problem: the aforementioned method was called exactly once. So
when my find panel was first shown before any documents, the buttons
were disabled (which is good) but they stayed disabled, regardless how
many documents were opened later on (not quite ok). Same the other way
round: (document, find panel, close all documents).
To make it work, one would probably have to subclass
NSDocumentController to inform the bindings system (via will change/
did change) whenever the status of currentDocument is going to change.
So I am doing it myself in the delegate of the find panel inside
windowDidUpdate: .
By the way there are these NSUserInterfaceValidations and
NSValidatedUserInterfaceItem protocols. Sound promising, but I don't
understand it. Looks like this is intended only for NSToolbarItem and
NSMenuItem; the latter of which does not need this, as there is
validateMenuItem already.
Kind regards,
Gerriet.
_______________________________________________
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