0

In Cocoa i want to create an nsbutton with delayed menu. i.e., When clicked it should call the action method and when kept in pressed state for 2 seconds it should display a nsmenu.

It is similar to "Build Active Target" button present in Xcode toolbar.

Regards,

Dhanaraj.

Dhanaraj
  • 987
  • 1
  • 10
  • 26
  • I have answered this question [here](http://stackoverflow.com/questions/9196109/nsbutton-with-delayed-nsmenu-objective-c-cocoa/39951734#39951734). [Code on my GitHub](https://github.com/evgenybaskakov/ButtonWithMenuOnLongClick) implements exactly that behavior based on a plain NSButton. – evgeny Oct 10 '16 at 05:31

1 Answers1

1

It's a NSPopUpButton.. Here is how I create it in my app.

NSToolbarItem* item = [[[NSToolbarItem alloc] initWithItemIdentifier:identifier] autorelease];
[item setLabel:label];
[item setPaletteLabel:label];
[item setToolTip:tooltip];

NSPopUpButton* button = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 24) pullsDown:NO] autorelease];
NSMenu* menu = [button menu];
// insert code here, that adds NSMenuItems to the menu

[button setTarget:self];
[button setAction:@selector(menuAction:)];
[[button cell] setBezelStyle:NSTexturedRoundedBezelStyle];
[[button cell] setArrowPosition:NSPopUpArrowAtBottom];
[[button cell] setFont:[NSFont systemFontOfSize:14]];
[item setView:button];
neoneye
  • 50,398
  • 25
  • 166
  • 151
  • How does it make it behave different when clicked/held for a second? – zrslv Mar 20 '12 at 04:13
  • I would like to know that too. I'm thinking that maybe the ButtonMadness sample from Apple might help. In that code, they have a DropdownButton which can display a menu below it. But it's just a regular NSButton, not an NSPopupButton. Perhaps with a delayed NSTimer or something it can display the menu instead of executing the button action if the mouseUp event hasn't occurred after a while. See the code here: https://developer.apple.com/library/mac/#samplecode/ButtonMadness/Introduction/Intro.html – Tap Forms Jul 28 '12 at 23:45