I’ve got a document-based app with a submenu off of the File menu that is context-sensitive. I’m trying to figure out the appropriate way to manage it, and I think I know the answer…but want to make sure I’m not missing something obvious/simpler.
The document has an NSWindowController that is instanced for each document, and is the NSWindowDelegate for that instance. In the NSWindowController‘s -init and -dealloc methods, I can create and remove the NSMenu. But since the menu is global to the app, and not specific to individual documents (or so I think), I also have to delete the menu and re-recreate it when switching documents via the NSWindowDelegate‘s -windowDidBecomeMain and -windowWillResignMain (or maybe the analogous …Key methods, I’m a little fuzzy on that distinction)
In addition, I need to make the NSWindowController the NSMenu‘s delegate, in order to implement -menuNeedsUpdate (and likewise, keep the delegate in sync with the currently active NSWindowController.
Is this the right approach?
One trick I’ve been using for context-sensitive menus is to make use of validateMenuItem to do more than just validate menuItems.
In your case, you could make the application delegate the menu’s delegate, and inside validateMenuItem, if the menu item is the one you need to change, then change it. Call [NSDocumentManager sharedManager] to get the instance of the currently active document, and get whatever info you need from that to populate your menu item.