I would like to create a popup menu using swift coding in a macOS app from a right click event using Xcode 11 or 12. Can't seem to find an example of this. I see plenty of IOS examples but none for macOS. Does anyone have an example of this you could share or could show me how this is done.
Also, I know how to add a menu to to the View Controller but this allows right click from anywhere in the tableView and I want the popup menu to show when you right click on a row in the table. If anybody is familiar with quicken for Mac I want the same function as it as when you right click on a row in an account register.
Here is the code for the right click event:
I have ask this question on couple of other forums as well as searched for tutorials on this and have come up empty.
Also, I know how to add a menu to to the View Controller but this allows right click from anywhere in the tableView and I want the popup menu to show when you right click on a row in the table. If anybody is familiar with quicken for Mac I want the same function as it as when you right click on a row in an account register.
Here is the code for the right click event:
Code Block // right click action override func rightMouseDown(with theEvent: NSEvent) { let point = tableView.convert(theEvent.locationInWindow, from: nil) let row = tableView.row(at: point) selectedCustomer = custviewModel.customers[row].custNumber! print(selectedCustomer) // Add popup menu print("right click") print(row) }
I have ask this question on couple of other forums as well as searched for tutorials on this and have come up empty.
I extract from an app, hope that will provide a good enough answer.
In this case, I create a popup menu, with values from absMin to absMax. or no value (any). Some values may be disabled. I did not edit in detail, so some parts are probably less useful for you.
Hope that helps anyway.
In rightMouseDown:
The popup creation
In this case, I create a popup menu, with values from absMin to absMax. or no value (any). Some values may be disabled. I did not edit in detail, so some parts are probably less useful for you.
Hope that helps anyway.
In rightMouseDown:
Code Block // Add popup menu let theMenu = popupMenuForValue(defaultVal: defaultValue, absMin: 1, absMax: 10) NSMenu.popUpContextMenu(theMenu, with: theEvent, for: self) // returns a selected value
The popup creation
Code Block @objc func setMaxi(_ sender: NSMenuItem) { selectedMaxFromMenu = sender.tag } func popupMenuForValue(defaultVal: Int, absMin: Int, absMax: Int) -> NSMenu { let theMenu = NSMenu(title: "Maxi") // Not used in fact theMenu.autoenablesItems = false // allows to deactivate some items let v0 = NSAttributedString(string: NSLocalizedString("Any", comment: "popupMenuForValue"), attributes: [NSAttributedStringKey.foregroundColor:NSColor.red]) let item0 = NSMenuItem(title: "v0", action: #selector(setMaxi(_:)), keyEquivalent: "") item0.attributedTitle = v0 item0.tag = 0 theMenu.addItem(item0) let itemSeparator = NSMenuItem.separator() theMenu.addItem(itemSeparator) if absMax == 0 { return theMenu } for i in 1...absMax { let basicString = String(i) var itemString : NSAttributedString if i == defaultVal { itemString = NSAttributedString(string: basicString, attributes: [NSAttributedStringKey.underlineStyle:NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.font: NSFont.boldSystemFont(ofSize: 12)]) } else { itemString = NSAttributedString(string: basicString, attributes: nil) } let item = NSMenuItem(title: String(i), action: #selector(setMaxi(_:)), keyEquivalent: "") item.attributedTitle = itemString item.tag = i if i < absMin { item.isEnabled = false } theMenu.addItem(item) } return theMenu }