Hel*_*You 2 cocoa menuitem swift
我想Menu-Object在 Swift 中以编程方式启用/禁用。我发现了一些东西Apple Developer Library:https : //developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/#//apple_ref/occ/instp/NSMenuItem/enabled
但是如何Menu-Object通过代码获取对 的引用并以编程方式设置其状态?
For Swift 4+, some of the code in Barath's Answer has become deprecated. I also think the code is more adaptable by searching for menuitem titles rather than their index:
let mainMenu = NSApplication.shared.mainMenu!
let subMenu = mainMenu.item(withTitle: "Edit")?.submenu
subMenu?.item(withTitle: "Cut")?.isEnabled = false
Run Code Online (Sandbox Code Playgroud)
Also, setting isEnabled to true or false will not toggle menu items as long as setAutoenablesItems is set to true. It can be disabled from the storyboard or programmatically.
| 归档时间: |
|
| 查看次数: |
3156 次 |
| 最近记录: |