I usesender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.onto let the user (un)check a menuitem.This state is saved in a file so the (un)checked menuitem can be restored when the user restarts the app.I hope this makes sense?
Post
Replies
Boosts
Views
Activity
Thank you for your reply, but I still can't get it to work.Please, see my response to OOPer.
I hope thiis example clarifies things:I have a user provided list of words, which the user classifiesWord class Noun group Subject Object Pronoun Personal IndefiniteI.e. Subject, Object, Personal, Indefinite may be (un)checked by the userand the word is stored along with the checked item's name(s) (sender.title)which I wish to use to restore the checkmark(s) for a given word.
I apologize for being so vague, but I hope the following helps:I forgot to mention I'm trying to make trial code for macOS. @IBOutlet weak var textResult: NSTextField! // Textbox for test purpose @IBAction func actionForItem(_ sender: NSMenuItem) { sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on textResult.stringValue = "Clicked on = " + sender.title // For test purpose// The following is hardcoded for test purpose let mainMenu = NSApplication.shared.mainMenu! let firstMenu = mainMenu.item(withTitle: "Word class")?.menu let myItemSubmenu = firstMenu?.item(withTitle: "Noun group")?.submenu // will be nil if there is not submenu (hierarchical) myItemSubmenu?.item(withTitle: "Subject")?.isEnabled = true }
I connect the menu action in IB by connecting to actionForItem(_ sender: NSMenuItem)and not elsewhere in code.You enable the menuItem in the IBAction: myItemSubmenu?.item(withTitle: "Subject")?.isEnabled = trueThe state before was Enabled (not greyed out) and without check mark.I would expect the code to put a check mark next to the item.
Is this what you are looking for:2019-12-08 22:33:32.117410+0100 Edgar[2669:436738] [Nib Loading] Failed to connect (object) outlet from (Edgar.AppDelegate) to (NSMenuItem): missing setter or instance variable2019-12-08 22:33:32.162887+0100 Edgar[2669:436738] Metal API Validation EnabledactionForItem Subject NSControlStateValue(rawValue: 1)The following is beyond my knowledge of XCode:Check also what image is defined for the menu item "Subject" in IB Attributs inspector for the on state field.