gpt4 book ai didi

swift - 在菜单栏子菜单中列出所有已安装的卷(仅以编程方式)

转载 作者:行者123 更新时间:2023-11-28 06:37:25 33 4
gpt4 key购买 nike

我目前正在做一个“仅菜单栏”项目,我需要在菜单栏应用程序的子菜单中列出所有已安装的卷。我想出了如何打印()所有已安装的卷,但我需要子菜单的帮助(没有 .xib 或 .storyboard 工作)这是我的“listVolumes func”

    func listVolumes(sender: NSMenuItem) {
let keys = [NSURLVolumeNameKey, NSURLVolumeIsRemovableKey, NSURLVolumeIsEjectableKey]
let paths = NSFileManager().mountedVolumeURLsIncludingResourceValuesForKeys(keys, options: [])
if let urls = paths {
for url in urls {
if let components = url.pathComponents
where components.count > 1
&& components[1] == "Volumes" {
print(url)
}
}
}
}

下面是我的“菜单栏应用程序”代码

    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)

func applicationDidFinishLaunching(aNotification: NSNotification) {

if let button = statusItem.button {
button.image = NSImage(named: "StatusBarButtonImage")
}
let menu = NSMenu()

menu.addItem(NSMenuItem(title: "Volumes", action: Selector("volumes:"), keyEquivalent: ""))
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem(title: "Help", action: Selector("help:"), keyEquivalent: ""))
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem(title: "Quit", action: Selector("terminate:"), keyEquivalent: "q"))

statusItem.menu = menu

}

所以我的问题是,如何创建一个包含所有已安装驱动器的子菜单(仅以编程方式)

最佳答案

例如,您必须为 volumes 菜单项设置子菜单

let volumesMenuItem = NSMenuItem(title: "Volumes", action: Selector("volumes:"), keyEquivalent: "")
menu.addItem(volumesMenuItem)
let volumesMenu = NSMenu(title: "Volumes")
volumesMenuItem.submenu = volumesMenu

我建议从您的 listVolumes() 函数返回一个名称数组,然后您可以将菜单项添加到 volumesMenu 假设 volumes包含名称

for volumeName in volumes {
volumesMenu.addItem(NSMenuItem(title: volumeName, action: Selector("selectVolume"), keyEquivalent: ""))
}

关于swift - 在菜单栏子菜单中列出所有已安装的卷(仅以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831507/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com