1
votes

I have 2 nib files, MainMenu.xib, and another owned by an NSWindowController subclass. I've got 4 choices on a window panel, where only one of them can be selected (if another is selected, I want to set a check mark on the new one, and clear the other 3)

So I can easily tell which menu item was picked, by routing everything through the First Responder, and setting tags on the 4 menu choices.

Then, the selector which receives this action just looks at the tag, and takes the desired action. And, as part of the IBAction, I get a reference to the sender...so I know how to set the checkmark on it. Clearing the checkmark on the other menu item could also be done by using an iVar to keep track of the most recent clicked on sender, and then I could do this, as suggested by the Cocoa docs:

[currentItem setState:NSOffState];
[sender setState:NSOnState];

Now the problem boils down to the fact this is a document style app. The menu selection is global, so I'd have to add logic whenever a new document window takes focus, and similar logic when a window loses focus.

I found a method I could implement -windowDidBecomeMain which tells me when my window controller became the main window. But I dont see a corresponding method that tells me that the old window lost focus so it can clean up.

2
What classes are those menu items? Why don't you just connect an IBOutlet to them? (Because that would be the simplest, best way)Vervious
I'd love to connect an IBOutlet to them. But how? In MainMenu.xib, I've got File's Owner (the NSApplication object), and the First Responder object...and thats pretty much it. When MainMenu is instantiated at launch, the controller that receives that IBAction doesnt even exist yet.wrjohns
I'm a little confused. Which controller receives the IBAction? (or which nib file does it live in?)Vervious
The controller is being created programmatically. If it were to be moved to a nib file, it cant be MainMenu.nib, because I've only got 1 instance of that, ever. And as a document style program, I could have many instances of the NSWindowController. I think I have a handle on how to approach this, I'll update the questionwrjohns

2 Answers

1
votes

I found the answer here.

The other methods are -windowDidResignMain and -windowWillClose

Edit: A better answer I had previously overlooked is to implement -(BOOL)validateMenuItem: I had seen this as a way of enabling or disabling menu items only via the return value, but I realized I could provide a side effect of setting the state as well. So the NSWindowController is set up as the window delegate. When the menu is opened, this method is called, and thus its always current for the active window.

0
votes

Create a new object (inheriting from NSObject) to act as your controller object. Instantiate that from the nib file and link the outlet to there.