当我右键单击NSTableView行时如何添加(显示)上下文菜单?

时间:2019-01-18 12:32:27

标签: swift macos contextmenu

我正在尝试使用一个tableView实例从NSViewController显示上下文菜单。

...
lazy var contextMenu: NSMenu = {
  let rightClickMenu = NSMenu()
  rightClickMenu.addItem(withTitle: "Add card", action: #selector(addCard), keyEquivalent: "")
  rightClickMenu.addItem(withTitle: "Remove card", action: #selector(removeCard), keyEquivalent: "")
  return rightClickMenu
}()

1 个答案:

答案 0 :(得分:1)

解决方案之一:

...
override func rightMouseDown(with event: NSEvent) {
        NSMenu.popUpContextMenu(contextMenu, with: event, for: self.view)
}
...