2010-02-04 80 views
1

我的工作,需要在各種情況下在屏幕上顯示上下文菜單的應用程序。在我正在寫的函數中,我無法訪問任何NSWindows或NSViews。我想使用popUpMenuPositioningItem:atLocation:inView,因爲這個函數在10.6中完全適合我。但是,我們需要支持10.5,所以這個功能對我來說是不可用的。尋找popUpMenuPositioningItem:atLocation:inView:相當於10.5

我最感興趣的,如文檔中陳述的特點是:

如果視圖是nil,地點是 屏幕解釋座標 系統。這允許你彈出一個從任何窗口斷開的 菜單。

基本上,我需要顯示給定的屏幕上的位置的上下文菜單,但沒有任何相關聯的視圖。

有沒有辦法在10.5上實現這個?

回答

0

我不知道該怎麼做,在可可,但你也許可以使用碳功能PopUpMenuSelect。

+0

謝謝,但我需要避免碳。另外,我相信PopUpMenuSelect已被棄用。 – 2010-04-07 19:55:38

1
// Set up the button cell, converting to NSView coordinates. The menu is 
// positioned such that the currently selected menu item appears over the 
// popup button, which is the expected Mac popup menu behavior. 
NSPopUpButtonCell* button = [[NSPopUpButtonCell alloc] initTextCell:@"" 
                  pullsDown:NO]; 
[button autorelease]; 
[button setMenu:menu_]; 
// We use selectItemWithTag below so if the index is out-of-bounds nothing 
// bad happens. 
[button selectItemWithTag:index]; 
[button setFont:[NSFont menuFontOfSize:fontSize_]]; 

// Create a dummy view to associate the popup with, since the OS will use 
// that view for positioning the menu. 
NSView* dummyView = [[[NSView alloc] initWithFrame:bounds] autorelease]; 
[view addSubview:dummyView]; 
NSRect dummyBounds = [dummyView convertRect:bounds fromView:view]; 

// Display the menu, and set a flag if a menu item was chosen. 
[button performClickWithFrame:dummyBounds inView:dummyView]; 

if ([self menuItemWasChosen]) 
    index_ = [button indexOfSelectedItem]; 

[dummyView removeFromSuperview];