Search code examples
objective-ccocoansbuttonnsmenu

When using `-popUpMenuPositioningItem:atLocation:inView:` to programmatically attach menus to buttons how do you get the selected menu item?


I have a button and in the button's action method I programmatically create a menu using, -popUpMenuPositioningItem:atLocation:inView:.

How can I get the menu item that the user selected? If I was doing this with NSPopUpButton it would be a simple case of calling selectedItem. However, NSButton nor NSMenu implement such behaviour.


Solution

  • You can hook up selectors and actions for each menu item, and then when you receive the action message you will know what was selected.

    By the way, I wonder why you don't use NSPopUpButton. You know you can change the style of NSPopUpButton to make it look like other buttons, yes?