Frage

I have a document-based application.

In my document class,

  • I have a BOOL property : isActionPossible.
  • I also have an IBAction : - (IBAction) doTheAction:(id)sender.

Now, in MainMenu.xib, I want to connect a menu item to the action. This is easy, when you know that the First Responder in Interface Builder "stands for" the active document of your application.

But how shall I do to bind the availability (the "enable property") of this item menu with the BOOL property isActionPossible of the active document ?

Indeed, in the list proposed, there isn't "First Responder".

War es hilfreich?

Lösung

Two choices:

  1. You can implement menu validation, see Apple's docs. In short before a menu is displayed the system sends a validation request to the same object that handles the action, which includes the first responder. You simply add a method to your document class which returns a BOOL; in your case your isActionPossible property.
  2. Use the currentDocument property of NSDocumentController which returns the document associated with the main window. You can then use bindings to bind <Document Controller>.currentDocument.isActionPossible. This approach is more general as you can bind label contents to values etc. as well as use it for menu bindings.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top