2

I have come across this Cocoa application (source code) that shows a main Window.

As long as this window is key it is possible to open the Preferences window from the Main menu as well as by hitting Command-, but when the main window is not key and another window from the same app is, the NSMenuItem is grayed out and the keyboard shortcut does not respond.

I've inspected the xib file associated to the Main Menu and that NSMenuItem is sending a openPreferences:(id)sender IBAction to the FirstResponder which sould be the NSApplication.

What am I missing (I am still a newbie at mac cocoa programming)? How can I fix it so that the preferences are reachable from each application window?

rano
  • 5,616
  • 4
  • 40
  • 66

1 Answers1

2

Probably the original author implemented - (BOOL)validateMenuItem:(NSMenuItem *)menuItem and returns NO under some circumstances.

NSMenuValidationProtocol documentation.

Update: Another quick guess: Maybe the object that handles the IBAction for the menu item is not in the responder chain anymore after you open the second window. NSMenuItems are only enabled if the action selector can be found in the responder chain.

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • I checked it on the whole project (textually) but it is never called nor implemented. – rano Nov 07 '10 at 09:45
  • Updated my original answer with another idea. – Thomas Zoechling Nov 07 '10 at 11:29
  • That second guess was what I was thinking as well. Probably the author implemented `openPreferences:` in the primary window or its window controller, rather than in something more global. So, any time the primary window (and its WC, if it has one) is not in the responder chain, nothing that is in it responds to that action, so any menu item that would send that action is disabled. – Peter Hosey Nov 07 '10 at 15:34
  • I hope that rano posts the solution. – Thomas Zoechling Nov 07 '10 at 18:46
  • I came to Peter Hosey's conclusion too (after reading a few pieces of documentation from Apple). To solve this issue you can just re-assign the sent action to `openPreferences:` from the correct (= the simple) controller (that implements it) and not via the FirstResponder. – rano Nov 09 '10 at 07:30
  • @Peter Hosey, only now I recognize you as the developer behind Adium and Growl : ) I really admire your works – rano Nov 09 '10 at 07:32
  • rano: I'm not *the* developer behind them. Both are mostly a lot of other people's work. But thank you from all of us. ☺ – Peter Hosey Nov 09 '10 at 14:29
  • I know : P, I was talking about your web page specifically and pardon me for my poor english – rano Nov 09 '10 at 18:41