我正在开发一个应用程序,它需要在各种情况下在屏幕上显示上下文菜单。在我编写的函数中,我无法访问任何NSWindows或NSViews。我想使用popUpMenuPositioningItem:atLocation:inView,因为这个函数在10.6中非常适合我。然而,我们有一个支持10.5的要求,所以这个函数对我来说是不可用的。
如文档中所述,我最感兴趣的特性是:
如果视图为零,则在屏幕坐标系中解释位置。这使您可以弹出与任何窗口断开连接的菜单。
基本上,我需要在屏幕上显示给定位置的上下文菜单,但不需要任何相关的视图。
有没有办法在10.5上实现这一点?
发布于 2010-12-23 04:22:01
// 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];发布于 2010-04-05 19:18:43
我不知道怎么用可可,但你可以用碳函数PopUpMenuSelect。
https://stackoverflow.com/questions/2203708
复制相似问题