Search code examples
cocoamacosmenuitemnsdocument

applicationDidFinishLaunching: executing after NSDocument is loaded from NIB


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?


Solution

  • 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.