The contextual nsmenuitem isn't enabled for the contextual menu for the menubar.

let item1 = NSMenuItem(title: "Reset", action: Selector(("resetAlarm:")), keyEquivalent: "R")
//item1.target = self
item1.isEnabled = true
let item2 = NSMenuItem(title: "Stop", action: Selector(("stopAlarm:")), keyEquivalent: "S")
//item2.target = self
item2.isEnabled = true
let item3 = NSMenuItem(title: "Pause", action: Selector(("pauseAlarm:")), keyEquivalent: "P")

// item3.target = self
item3.isEnabled = true
let item4 = NSMenuItem(title: "Continue", action: Selector(("continueAlarm:")), keyEquivalent: "C")
//item4.target = self
item4.isEnabled = true
ContextualMenu.update()
ContextualMenu.addItem(item1)
ContextualMenu.addItem(item2)
ContextualMenu.addItem(item3)
ContextualMenu.addItem(item4)

statusbar.menu = ContextualMenu
statusbar.button?.isEnabled = true
ContextualMenu.autoenablesItems = true
NSApp.mainMenu?.update()


Why is the contextual menu items not enabled? Any tips...

Replies

Usually menu items are disabled because they're not connected to anything. My guess is your menu items have the wrong target.


You have commented out code where you set the target to self. What is self? You could try setting the target to the first responder. If that doesn't work you will have to supply more information about the classes in your project for anyone to tell you the right target to use for the menu items.

I am trying to connect the target to call a function in the same ViewController class.

I know that when working in Interface Builder with storyboards, making connections from menu items to a view controller does not work. The actions for the menu items do not appear. See the following article:


meandmark.com/blog/2015/02/connecting-menu-items-to-ibactions-in-a-mac-storyboard/


Apple puts links into moderation so you'll have to paste the URL.


So you're not going to be able to set the target to a view controller. In my experience with Interface Builder, connecting the menu items to First Responder worked.

I am trying to do everything at runtime instead of using storyboards. Do you have any thoughts on that?

Setting the target is the coding equivalent to making a connection in Interface Builder. Set the target for the menu items to something other than the view controller. I've already suggested you set the target to the first responder.


If setting the target to the first responder doesn't work, you're going to have to provide more information about the user interface for your app.

What is the first responder in code not using Storyboard or Interface Builder?

The app and the window for your view controller have a first responder. Look at the NSResponder class, which is how Mac apps handle events and process commands, such as handling menu item selection. Also do some research on Cocoa's responder chain. An Internet search for Cocoa responder chain turns up many helpful articles.


I'm also aware you're not using Interface Builder or storyboards. You don't have to tell me every time you respond.