« Santa Cruz Guerilla Drive-In | Main | Discovery HD -- Weird Homes »
July 08, 2005
Adding a popup menu to any NSButton
It is easy to add a popup to any button (NSButton).
Here are the steps:
- In your nib, drop down an NSPopupButton and add the items you want to its popup list.
- Set the NSPopupButton to be hidden.
- Add an outlet for the NSPopupButton (named popupButton in this case)
- Drop down an NSButton.
- Set the image (or whatever else you want) for the button
- In the action for the button, run the following code:
[[popupButton cell] performClickWithFrame:[sender frame] inView:[sender superview]];
That is it!
Posted by corbin at July 8, 2005 04:07 PM
Comments
At least with Tiger the NSPopupMenu sets one menu item to the enabled state, untoggleable in IB. Any easy solution to this?
Thanks for the tip! :-)
Joern.
Posted by: Joern at July 10, 2005 03:07 AM
Joern: can you give an example of what you mean? You might have to call the validation routine on the menu before poping it up.
Posted by: corbin at July 10, 2005 09:55 AM
Hum, for the record, it's not a pop-up, it's a pop-down.
Posted by: Darth Sidious at July 11, 2005 01:22 PM
It's a pull-down, dumbass.
Posted by: Michael Ellis at July 12, 2005 04:59 AM
Any idea if this is compatible with the human computer interface guidelines of Mac OS X? I dislike this kind of GUI-obstruction (even by Apple), get the standard GUI done then you are a champ! Pull-down, Pop-up, doesn't matter, only thing that maters is ease of use, man.
Posted by: anonymousSceptical at July 12, 2005 05:13 AM
To prevent the selection checkmark from appearing, just add this to the top of your action method:
[popupButton selectItem: nil];
This will remove the selection check before displaying the menu.
Thanks Corbin. This is much easier than what I was doing to make Apple-like popup buttons.
Posted by: transom at July 12, 2005 10:38 AM
Thanks, anonymousSceptical, that's the solution. :-)
Posted by: Joern at July 13, 2005 05:59 AM
Thanks for the tip.
To get the menu to also appear when you simply hold the mouse down (as in Mail and Finder), I found I had to also do:
[button sendActionOn:NSLeftMouseDownMask];
where button is the visible NSButton (not the NSPopUpButton).
Posted by: Randy Saldinger at July 13, 2005 09:47 PM