1

Maybe I'm coming at this the wrong way, but in my application I have loadable bundles that add NSMenuItem entries to the main menu.

I loop over all the bundles and determine what menu items to add, then add them accordingly.

Now the question is, where should such code go in a document-based application?

Initially I put it in my NSDocument subclass' -windowControllerDidLoadNib: method, but this had the undesired side-effect of repeating the same items n times, where n is the number of documents I've opened (i.e. it's not a run-one place to put the code).

So then I tried putting the code in my NSApplicationDelegate's -applicationDidFinishLaunching: method, which does mean it only runs once, but I'm facing an issue where the document springs up and displays before -applicationDidFinishLaunching: executes (or at least before it finishes).

Where's generally the right place to put code that dynamically adds menu items to a document-based application on startup?

d11wtq
  • 34,788
  • 19
  • 120
  • 195

1 Answers1

1

The -windowControllerDidLoadNib: method is called for each document that's created/opened. This is more application-level so documents shouldn't be worrying about this.

How about using the -menuNeedsUpdate: delegate method to update the menu at display time? That way, your application can simply maintain the list it has created at launch and keep the menu updated. Alternatively, you could put it in your app delegate's -awakeFromNib method.

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • -menuNeedsUpdate: looks helpful, thanks. But how does this play in a multi-document application (such as mine), where each document wants to make changes to the menu? Seems quite limiting that it's a delegate and not a notification so more than one object can respond. I need to adjust the states of various menu items as the user switches between documents. – d11wtq Nov 04 '10 at 15:09
  • Scratch that. Looks like `NSMenuDidBeginTrackingNotification` is sent. My bad :) – d11wtq Nov 04 '10 at 15:11
  • So your document *does* have a say over the plugin menu's state? I think you need to clarify your question with a bit more detail. – Joshua Nozzi Nov 04 '10 at 15:20
  • Hi... my original question was purely about where to "add" items to the menu on startup. I want all the items to always be navigable, but disabled until a document activates them. My question has sort of changed now into one about enabling/disabling menu items as the user switches between documents. I should start a new question, and will, thanks :) – d11wtq Nov 04 '10 at 22:33
  • New question added here: http://stackoverflow.com/questions/4102096/multiple-documents-open-at-the-same-time-each-with-different-menu-item-states – d11wtq Nov 04 '10 at 22:42