Reputation: 67
so i've been trying to add items to the right click menu when u right click on the app icon in the dock in macos
but when i do it with this code whenver i right click the app icon it re-adds the menu item
class myclass: NSObject , NSApplicationDelegate{
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}
Upvotes: 1
Views: 945
Reputation: 285290
You have to create the NSMenu
inside the method
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
let dockMenu = NSMenu()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}
or remove the items
let dockMenu = NSMenu()
func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
dockMenu.removeAllItems()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}
However I would declare it in Interface Builder and use an outlet.
Upvotes: 0