I am looking for some code to make a Menu appear next to a button when it is clicked. What code would I need for this?
Sorry if this sounds a bit vague.
I am looking for some code to make a Menu appear next to a button when it is clicked. What code would I need for this?
Sorry if this sounds a bit vague.
If you really need to roll this yourself, rather than using one of the built-in controls that shows a menu, you can create an NSPopupButtonCell and use that to show the NSMenu:
NSPopUpButtonCell *popupCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:YES];
[popupCell setMenu:yourMenu];
[popupCell trackMouse:event inRect:[yourButton bounds] ofView:yourButton untilMouseUp:YES];
[popupCell release];
You'd want to adjust the pullsDown:, inRect:, and ofView: arguments as necessary to position the menu the way you want.
NSPopupButton was my first thought as well. It's how apps with the "action gear" buttons accomplish their menus.
If you do have something else in mind though, look at NSMenu's +popUpContextMenu:withEvent:forView:
. Just hook an action method up to your button, create an NSMenu and populate it with NSMenuItems, and send it to this method along the the current event from NSApplication's currentEvent
getter.