Cannot seem to setEnabled:NO on NSMenuItem

后端 未结 8 1513
故里飘歌
故里飘歌 2021-02-12 10:51

I have subclassed NSMenu and connected a bunch of NSMenuItem\'s via Interface Builder. I have tested via the debugger to see that they really get initi

8条回答
  •  我寻月下人不归
    2021-02-12 11:02

    Swift 3 answer:

    I have a submenu item under the standard "View" menu called "Enable System Setup On Launch". I use the function below to enable or disable the menu item. Note: the view menu does need the "Auto Enable Items" in IB to be turned off.

    func enableSystemSetupMenuItem(enabled:Bool) {
        //set view menu item to enabled: value
        //requires "Auto Enable Items" of "View" menu item to be turned off in IB
        //because "View" menu is now turned off for "Auto Enable" we have to handle all 
        //of the "View" menu items ourselves
        //just to avoid operating on menu separators I set all other menu items to TAG = -1
        let main = NSApplication.shared().menu?.item(withTitle: "View")
        let subMenuItems = main?.submenu?.items
        for item in subMenuItems! {
            if item.title == "Enable System Setup On Launch" {
                item.isEnabled = enabled
            } else if item.tag == -1 {
                item.isEnabled = true
            }
        }
    }
    

提交回复
热议问题