在可可OSX编程实例化一个NSPopUpButtonCell
-
20-09-2019 - |
题
我有一个OpenGL GUI接口和I需要有当在我的openGL的显示按下该特定按钮将被称为一个弹出菜单。我想显示类似当你按下一个NSPopUPButton但我不能用可可按钮本身弹出一个菜单。看来,做到这一点的最好办法是使用NSPopupButtonCell。我不能在这里发布我的代码,因为我写在Lisp语言的代码,并通过目标C桥访问可可,但我只是想看看下面的pseduo代码看起来像它应该显示菜单或是否有一个明显的缺陷在我逻辑:
• 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.
这些方法中没有成功地获得任何种类的菜单的显示。有一些其他的方式来程式设计弹出包含在小区中的菜单?
解决方案
我认为你正在寻找的 trackMouse:inRect:ofView:untilMouseUp:
的NSCell
方法
[theCell trackMouse:[NSApp currentEvent] inRect:NSZeroRect ofView:theView untilMouseUp:YES];
其他提示
我想你会过得更好使用纯NSMenu
并调用+[NSMenu popUpContextMenu:withEvent:forView:]
。如果你只靶向10.6及更高版本,您可能还检查出-[NSMenu popUpMenuPositioningItem:atLocation:inView:]
,它使您可以在菜单的定位多一点控制。只实现mouseDown:
在你看来,构造菜单(或从笔尖文件加载),并显示它,并NSMenu
应该从那里的所有细节。只要确保每个NSMenuItem
的目标和动作被设定为使得当选择的项目的操作方法被正确调用。
不隶属于 StackOverflow