Problem : the items are disabled and I don't know how to enable it :
What's wrong ?
To make menu items enabled, there needs to be a responder which can respond to the action
.
class ViewController: NSViewController {
@IBOutlet weak var searchField: NSSearchField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let theMenu = NSMenu(title: "Filters")
let theMenus = ["all", "artist", "title", "album"]
for theName in theMenus {
//↓ Specify `action:`.
let theMenuItem = NSMenuItem(title: theName, action: #selector(menuItemSelected), keyEquivalent: "")
theMenu.addItem(theMenuItem)
theMenuItem.isEnabled = true
}
searchField.searchMenuTemplate = theMenu
}
//↓ Define the `action` method used in `NSMenuItem(title:action:keyEquivalent:)`.
@objc func menuItemSelected(_ sender: Any) {
print(sender, type(of: sender))
}
//...
}
Generally, you should better show more context (especially your code), to get better responses sooner.