10

I have an NSMenu which opens when a user clicks an NSStatusItem in the status bar. How can I determine if the NSMenu is opened (expanded) or not?

Thank you!

Knodel
  • 4,359
  • 8
  • 42
  • 66

1 Answers1

14

You can use the NSMenuDelegate protocol to handle menuWillOpen and menuWillClose messages. Use menuWillOpen to set a boolean that states that the menu is open, and menuWillClose to turn it off.


Update: Looks like menuWillClose: is no longer an available delegate method. Use menuDidClose: instead.

Itai Ferber
  • 28,308
  • 5
  • 77
  • 83
  • Thank you! But it still doesn't work. What I did is add to the interface file. Then in .m file I added two methods: `-(void) menuWillOpen:(NSMenu *) theMenu { isClosed = NO; }` and `-(void) menuWillOpen:(NSMenu *) theMenu { isClosed = NO; }` Is that enough? What did I do wrong? – Knodel Jan 07 '11 at 20:14
  • 1
    You have to call `[menu setDelegate:self]` in your `awakeFromNib` method or something. – Itai Ferber Jan 08 '11 at 01:21
  • No problem! I've made the mistake of forgetting to set delegates many a time, often with bizarre results... good luck! ;) – Itai Ferber Jan 08 '11 at 17:00
  • Due to the current Mac Developer Library there is no `menuWillClose` method but a `menuDidClose:` method instead. Have a look at https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSMenuDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSMenuDelegate – anka May 01 '13 at 12:16