0

How can I detect when the menu in menu bar is clicked? I mean when whatever is clicked like for example "file" menu not a concrete position in the menu.

I tried to connect IBActions from menu items in interface builder from my code, but it does not work from the main menu item like "File" only from the submenus within this menu

Wojtek
  • 1,006
  • 11
  • 30

2 Answers2

0

First of all, what are you trying to achieve in the big picture? Perhaps knowing when the menu bar has been clicked is not the best approach.

You might be able to achieve what you're interested in by observing the NSMenuDidBeginTrackingNotification notification, but it's hard to tell. For example, this will fire for keyboard-initiated tracking, too.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

Go to MainMenu.xib and look at the menu layout. It will be something like

Main Menu

Menu Item - File

Menu - File

Right click on The NSMenu you want to observe, not the NSMenuItem. attach the delegate to an object or controller of your choosing.

Then implement

- (void)menuWillOpen:(NSMenu *)menu
lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • I already tired it but it's not working for me. What I did is set delegate to File's owner added this method but it doesn't get fired. – Wojtek Jun 01 '14 at 14:29
  • I also added NSMenuDelegate to mile .h file – Wojtek Jun 01 '14 at 14:35
  • It worked for me. Did you attach the delegate of the NSMenu called 'File'? You can't attach it to the MainMenu or Menu Item called 'File' – lead_the_zeppelin Jun 01 '14 at 14:35
  • Yes, I did. I have to attach it to the File's owner, don't I? – Wojtek Jun 01 '14 at 14:42
  • you can attach it to any controller you want. Usually I would attach it to the first responder. Since it makes the most sense. – lead_the_zeppelin Jun 01 '14 at 14:44
  • I don't think it makes sense to attach a delegate outlet to the First Responder. First, it's a delegate, not a target for an action. Second, the First Responder basically means `nil`. That is, the target for a control which is configured to send an action to First Responder is `nil`. – Ken Thomases Jun 01 '14 at 15:37
  • @KenThomases you are right. I think I might have meant it makes sense to implement all menu functionality in whatever is the first responder. – lead_the_zeppelin Jun 01 '14 at 17:47