views:

26

answers:

1

I would like three menu items on the menubar with the keyboard shortcuts cmd-1, cmd-2, cmd-3. This I know how to do.

Each menu item would open up a different window (win1, win2, win3).

I want it so that only one instance of each window is permitted to be open at any one time (i.e. only one win1, one win2, etc).

How is this best approached?

+1  A: 

If the windows in question are in the MainMenu.nib it's simple; attach the action to -makeKeyAndOrderFront: on each window.

If, on the other hand, they are not in MainMenu.nib, as is more likely the case if your application is structured in a sane fashion; things get a little more complicated. Long story short; you need to attach the menu item actions to appropriate methods on some manner of controller object (most likely your application delegate, although any controller that "sees" all the required nibs will do); and then have the controller in question send a similar message to its window.

This is generally a sensible approach, as you can have smaller controller objects attached to your windows that also act, if appropriate, as data sources for the various views in the windows in question.

It also allows for lazy loading of the windows, which is, at last count, a Good Thing™.

If this is some manner of document-oriented (not necessarily document-based) application, and the windows display some attribute of the currently selected "document" or piece of data; subclassing NSWindowController and loading the window controllers in your MainMenu.nib is probably a good place to start.

Note: If the objects responsible for controlling the windows live in the responder chain, they don't even need to be referenced in MainMenu.nib; you can just attach the appropriately-named IBActions (e.g. openDetailsWindow: or the like) used to open the windows to the virtual FirstResponder object. (Simply add the selectors to its list of known methods, and you're golden.)

Reedit: To make the window not appear in the windows menu, you can call [window setExcludedFromWindowsMenu:YES].

To check/uncheck the menu item is a bit tricker, as it requires your window controller actually knowing about the menu item; but as long as this is the case, it's quite simple, again; call -setState:, with the relevant state names (NSOffState, NSOnState), for example from the delegate methods called when the window is shown/closed. (This could, again, be encapsulated "inside" the application delegate; if you for whatever reason don't want your MainMenu.nib to contain the window controllers.)

Williham Totland
Suppose I have three nibs (one for each window) and my app delegate has three methods (openWinA, openWinB, openWinC). If I attach the each menu item's action to the corresponding method to load the window, how would I implement greying out the menu item UNTIL it's matching window is subsequently closed (as I mentioned, I only want one instance of each window). P.S. I've sorted my accept rate :-)
Garry
On a related note. If you take a look in iTunes Window menu item there are 2 windows you can launch (iTunes and Equaliser). When either (or both) of these windows are displayed, a tick appears next to them to indicate this. Also, the title of these two windows does not, in addition, appear again in the Window menu item. How would I approach mimicking this?
Garry
Answer updated; I suggest you distill the comments into your original question (using the Edit button). Makes it easier for people to find the information later. :)
Williham Totland