0

我有一个 openGL GUI 界面,我需要一个弹出菜单,当在我的 openGL 显示中按下某个按钮时将调用该菜单。我想显示一个类似于按下 NSPopUPButton 时弹出的菜单,但我不能使用 Cocoa 按钮本身。似乎最好的方法是使用 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.

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

4

2 回答 2

3

我想你正在寻找的trackMouse:inRect:ofView:untilMouseUp:方法NSCell

[theCell trackMouse:[NSApp currentEvent] inRect:NSZeroRect ofView:theView untilMouseUp:YES];
于 2009-12-18T13:33:48.293 回答
0

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

于 2009-12-18T06:10:53.303 回答