首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找popUpMenuPositioningItem:atLocation:inView:等效于10.5

查找popUpMenuPositioningItem:atLocation:inView:等效于10.5
EN

Stack Overflow用户
提问于 2010-02-04 22:26:44
回答 2查看 666关注 0票数 1

我正在开发一个应用程序,它需要在各种情况下在屏幕上显示上下文菜单。在我编写的函数中,我无法访问任何NSWindows或NSViews。我想使用popUpMenuPositioningItem:atLocation:inView,因为这个函数在10.6中非常适合我。然而,我们有一个支持10.5的要求,所以这个函数对我来说是不可用的。

如文档中所述,我最感兴趣的特性是:

如果视图为零,则在屏幕坐标系中解释位置。这使您可以弹出与任何窗口断开连接的菜单。

基本上,我需要在屏幕上显示给定位置的上下文菜单,但不需要任何相关的视图。

有没有办法在10.5上实现这一点?

EN

回答 2

Stack Overflow用户

发布于 2010-12-23 04:22:01

代码语言:javascript
复制
// 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];
票数 1
EN

Stack Overflow用户

发布于 2010-04-05 19:18:43

我不知道怎么用可可,但你可以用碳函数PopUpMenuSelect。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2203708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档