2010-07-22 44 views
0

在可可我想用延遲菜單創建nsbutton。 即,當點擊時應該調用動作方法,並且當保持在按下狀態2秒鐘時,它應該顯示一個nsmenu。如何用延遲的NSMenu創建NSButton?

它與Xcode工具欄中的「Build Active Target」按鈕類似。

Regards,

Dhanaraj。

+0

我已經回答了這個問題[這裏](http://stackoverflow.com/questions/9196109/nsbutton-with-delayed-nsmenu-objective-c-cocoa/39951734#39951734)。 [在我的GitHub上的代碼](https://github.com/evgenybaskakov/ButtonWithMenuOnLongClick)基於普通的NSButton實現了完全的行爲。 – evgeny 2016-10-10 05:31:53

回答

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

當點擊/保持一秒時它是如何表現不同的? – zrxq 2012-03-20 04:13:48

+0

我也想知道。我在想,也許Apple的ButtonMadness示例可能會有所幫助。在該代碼中,他們有一個DropdownButton,它可以在其下面顯示一個菜單。但它只是一個常規的NSButton,而不是NSPopupButton。也許延遲的NSTimer或其它東西可以顯示菜單,而不是在一段時間後沒有發生mouseUp事件時執行按鈕操作。請參閱此處的代碼:https://developer.apple.com/library/mac/#samplecode/ButtonMadness/Introduction/Intro.html – 2012-07-28 23:45:39