我有一个带有自定义视图的NSMenu,就像这样。
ViewController.swift
class ViewController: NSViewController {
@IBOutlet var theMenu: NSMenu!
@IBOutlet var theMenuItem: NSMenuItem!
@IBOutlet var customView: NSView!
override func viewDidLoad() {
theMenuItem.view = customView
}
@IBAction func showTheMenuWithCustomView(_ sender: NSButton) {
// Popup the menu below button
let position = NSPoint(x: 0, y: sender.frame.height+2)
theMenu.popUp(positioning: nil, at: position, in: sender)
}
}
这很有效。
但在自定义视图中,我需要弹出另一个菜单。在我调用popUp方法之后,它什么也没显示。这是代码:
CustomView.swift
class CustomView: NSView {
@IBAction func showCustomViewMenu(_ sender: NSButton) {
// Create the menu in custom view
let customViewMenu = NSMenu()
// Add a menu item in it
customViewMenu.addItem(withTitle: "no one care", action: nil, keyEquivalent: "")
// Popup the menu below button
let position = NSPoint(x: 0, y: sender.frame.height+2)
customViewMenu.popUp(positioning: nil, at: position, in: sender)
}
}
您可以克隆此项目以进行测试:
https://github.com/Caldis/NSMenuQuestion
如何在NSMenuItems的自定义视图中弹出NSMenu?
答案 0 :(得分:0)
使用此行显示CustomView上的其他弹出窗口:
NSMenu.popUpContextMenu(customViewMenu, with: NSApp.currentEvent!, for: sender)
此外,如果您还需要在整个CustomView
上停用其他弹出窗口,请覆盖rightMouseDown
中的CustomView
,如下所示:
class CustomView: NSView {
override func rightMouseDown(with event: NSEvent) {
// Do nothing
}
}