Since applicationDockMenu:
is a delegate method, having an instance method add menu items would conflict with the delegate return.
What you could do is make the dock menu a property/instance variable in your application delegate class. This way, your view controller could modify the menu either by passing the reference to the menu from your application delegate to your view controller (which you would have a dockMenu
property) or referencing it globally (less recommended).
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var dockMenu = NSMenu(title: "MyMenu")
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let viewController = ViewController(nibName: "ViewController", bundle: nil) {
viewController.dockMenu = self.dockMenu
self.window.contentViewController = viewController
}
}
func applicationDockMenu(sender: NSApplication) -> NSMenu? {
return self.dockMenu
}
class ViewController: NSViewController {
var dockMenu: NSMenu?
// Button action
@IBAction func updateDockMenu(sender: AnyObject) {
self.dockMenu?.addItem(NSMenuItem(title: "An Item", action: nil, keyEquivalent: ""))
}
}