ViewController.swift
import Cocoa class ViewController: NSViewController , NSWindowDelegate { @IBOutlet weak var menuButton: NSButton! @objc func rightClick(_ sender:ClickRecognizer){ self.selectData = sender.data let theMenu = popupMenuForValue(data:sender.data) NSMenu.popUpContextMenu(theMenu, with:NSApp.currentEvent!, for: sender.view! ) } override func viewDidLoad() { let ges = ClickRecognizer() ges.target = self ges.data = "u07500499" ges.buttonMask = 0x2 ges.numberOfClicksRequired = 1 ges.action = #selector(rightClick(_:)) menuButton.addGestureRecognizer(ges) } }
ClickRecognizer
import Cocoa class ClickRecognizer: NSClickGestureRecognizer { var data:String = "" }