Skip navigation.
 
mlRe: Binding NSMenuItem state in Document-based app
FROM : Quincey Morris
DATE : Mon Jun 09 21:57:15 2008

On Jun 9, 2008, at 11:07, Steve Nicholson wrote:

> I have a document-based app in which I'm trying to bind the state of 
> NSMenuItems to values in my document's window controller. For 
> example, in the window's nib file, I have a checkbox bound to File's 
> Owner/autoscaleX. I'd like the menu item to have the same 
> functionality as that check box and to display the status for 
> whichever window is currently selected.
>
> Since File's Owner is NSApplication in MainMenu.nib I get an 
> exception when I try to bind to autoscaleX. I tried adding an 
> autoscaleX action to FirstResponder and binding to that, but I still 
> get the exception.
>
> I also tried not binding the menu item and just setting its action 
> to FirstResponder's autoscaleX action, but the menu item remains 
> disabled.
>
> I'm sure there's an easy Cocoa way to do this, but I haven't been 
> able to dig it up in the docs or my searches.


Doing this through bindings involves re-inventing a bit of stuff that 
NSResponder normally takes care of, but it need not be too difficult. 
For example, you could:

-- Create an application delegate, if you don't have one already.

-- Give the application delegate a -
(NSWindowController*)currentDocWinController property.

-- In your window's delegate (the NSDocument or NSWindowController, 
depending how it's set up, or possibly even something else), 
implement  windowDidBecomeMain and/or windowDidBecomeKey to invoke 
'appDelegate. currentDocWinController = <the relevant window's 
controller>', and windowWillClose to invoke 'appDelegate. 
currentDocWinController = nil'.

-- Bind the menu item's state to File's Owner.delegate. 
currentDocWinController.autoscaleX

The hardest part is step 3, since it must maintain the value of the 
property correctly, and do so in a KVO-compliant manner, and avoid 
race conditions when there might be multiple windows opening or closing.

Or something like that.

Related mailsAuthorDate
mlBinding NSMenuItem state in Document-based app Steve Nicholson Jun 9, 20:07
mlRe: Binding NSMenuItem state in Document-based app Quincey Morris Jun 9, 21:57
mlRe: Binding NSMenuItem state in Document-based app Jerry Krinock Jun 10, 00:04
mlRe: Binding NSMenuItem state in Document-based app Steve Nicholson Jun 10, 16:45