【问题标题】:Programmatically instantiating a NSPopUpButtonCell in Cocoa OSX在 Cocoa OSX 中以编程方式实例化 NSPopUpButtonCell
【发布时间】:2020-02-16 17:00:01
【问题描述】:

我有一个 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.

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

【问题讨论】:

    标签: cocoa macos nspopupbuttoncell


    【解决方案1】:

    我认为您正在寻找NSCelltrackMouse:inRect:ofView:untilMouseUp: 方法

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我的目标是支持 10.5 或更早版本的机器,所以我不能使用 popUpMenuPositioningItem:atLocation:inView:。我也尝试过使用 popUpContextMenu:withEvent:forView: 但由于整个目标 c 桥我没有有一个真正的 NSEvent 所以我不得不伪造一个鼠标按下事件,由于某种原因它忽略了我传递给我使用 mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure: no 创建的事件的点无论我将什么坐标传递给位置参数,菜单都会显示在左下角
      • 另外,cocotron 似乎不支持这种方法,我们也在尝试以 windows 为目标。
      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多