在Cocoa OSX中以编程方式实例化NSPopUpButtonCell

在Cocoa OSX中以编程方式实例化NSPopUpButtonCell,cocoa,macos,nspopupbuttoncell,Cocoa,Macos,Nspopupbuttoncell,我有一个openGL GUI界面,我需要有一个弹出菜单,当在我的openGL显示器中按下某个按钮时,将调用该菜单。我想显示一个菜单,类似于当您按下NSpoupButton时弹出的菜单,但我不能使用可可按钮本身。似乎实现这一点的最佳方法是使用NSPopupButtonCell。我不能在这里发布我的代码,因为我正在用Lisp编写代码,并通过Objective-C桥访问Cocoa,但我只是想看看下面的伪代码是否应该显示菜单,或者我的逻辑中是否存在明显的缺陷: • Pseudo Code for fun

我有一个openGL GUI界面,我需要有一个弹出菜单,当在我的openGL显示器中按下某个按钮时,将调用该菜单。我想显示一个菜单,类似于当您按下NSpoupButton时弹出的菜单,但我不能使用可可按钮本身。似乎实现这一点的最佳方法是使用NSPopupButtonCell。我不能在这里发布我的代码,因为我正在用Lisp编写代码,并通过Objective-C桥访问Cocoa,但我只是想看看下面的伪代码是否应该显示菜单,或者我的逻辑中是否存在明显的缺陷:

• Pseudo Code for function that is called when button is pressed:
• Initialize an NSPopUpButtonCell Using initTextCell: "test" pullsDown: NO
• allocate an NSMenu using alloc
• add two menu items using addItem:
• set the menu of the popUpButtonCell to be the menu that was just allocated using setMenu:
• Next I tried two methods to try and get the menu to actually be displayed,
  first I tried using drawWithFrame:inView: and when that did not work I also tried 
  using drawBezelWithFrame:inView: eventhough the docs said not to use but I just 
  tried it out of desperation.
• finally, when the draw methods did not work I tried using performClick: on the 
  popupButtonCell to try and simulate the click.

这些方法都无法成功地显示任何类型的菜单。是否有其他方法以编程方式弹出单元格中包含的菜单?

我认为最好使用普通的
NSMenu
并调用
+[NSMenu-popUpContextMenu:withEvent:forView:
。如果您的目标仅为10.6及更高版本,您还可以查看
-[NSMenu POPUPMENUPPOSITIONGITEM:atLocation:inView:][/code>,这样您就可以更好地控制菜单的位置。只需实现
mouseDown:
在您的视图中,构造一个菜单(或从nib文件加载),并显示它,
NSMenu
就可以处理其中的所有细节。只需确保为每个
NSMenuItem
设置了目标和操作,以便在选择项目时正确调用操作方法。

我认为最好使用普通的
NSMenu
并调用
+[NSMenu popUpContextMenu:withEvent:forView:
。如果您的目标仅为10.6及更高版本,您还可以查看
-[NSMenu POPUPMENUPPOSITIONGITEM:atLocation:inView:][/code>,这样您就可以更好地控制菜单的位置。只需实现
mouseDown:
在您的视图中,构造一个菜单(或从nib文件加载),并显示它,
NSMenu
就可以处理其中的所有细节。只需确保为每个
NSMenuItem
设置了目标和操作,以便在选择项目时正确调用操作方法。

我想您正在寻找
NSCell

[theCell trackMouse:[NSApp currentEvent] inRect:NSZeroRect ofView:theView untilMouseUp:YES];

我认为您正在寻找
NSCell

[theCell trackMouse:[NSApp currentEvent] inRect:NSZeroRect ofView:theView untilMouseUp:YES];

我的目标是支持10.5或更早版本的机器,因此我无法使用PopupMenuPositionGitem:atLocation:inView:。我也尝试过使用popUpContextMenu:withEvent:forView:但由于整个objective c桥,我没有一个真正的NSEvent,所以我不得不假装鼠标按下事件,出于某种原因,它忽略了我正在传递到我使用mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure创建的事件:无论传递给location参数的坐标是什么,菜单也始终显示在左下角,cocotron似乎不支持此方法,我们也尝试将windows作为目标。我的目标是支持10.5或更早版本的计算机,因此我无法使用PopupMenuPositionGitem:atLocation:inView:。此外,我还尝试使用popUpContextMenu:withEvent:forView:但由于整个objective c桥,我没有真正的NSEvent因此,我不得不假装一个鼠标按下事件,出于某种原因,它忽略了我传递到我使用mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure创建的事件中的点,无论我传递给location参数的坐标是什么,菜单将始终显示在左下角。而且,cocotron似乎不支持这种方法,我们也在尝试以windows为目标。