【问题标题】:How to create NSButton with delayed NSMenu?如何使用延迟的 NSMenu 创建 NSButton?
【发布时间】:2011-03-19 13:14:21
【问题描述】:

在 Cocoa 中,我想创建一个带有延迟菜单的 nsbutton。 即,当点击时它应该调用操作方法,并且当它保持按下状态 2 秒时它应该显示一个 nsmenu。

类似于 Xcode 工具栏中的“Build Active Target”按钮。

问候,

达纳拉杰。

【问题讨论】:

  • 我已经回答了这个问题hereCode on my GitHub 正是基于一个普通的 NSButton 实现了该行为。

标签: cocoa nsmenu nsbutton


【解决方案1】:

这是一个 NSPopUpButton。这是我在我的应用中创建它的方法。

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];

【讨论】:

  • 点击/按住一秒钟后它的行为有何不同?
  • 我也想知道。我在想 Apple 的 ButtonMadness 样本可能会有所帮助。在该代码中,他们有一个 DropdownButton 可以在其下方显示一个菜单。但它只是一个普通的 NSButton,而不是 NSPopupButton。也许使用延迟的 NSTimer 或其他东西,如果 mouseUp 事件在一段时间后没有发生,它可以显示菜单而不是执行按钮操作。在此处查看代码:developer.apple.com/library/mac/#samplecode/ButtonMadness/…
猜你喜欢
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 2015-02-15
相关资源
最近更新 更多