本文介绍了是否可以在NSPopupButton中对项目进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示系统声音列表,但我想按地区对它们进行分组.

I'm trying to display a list of system voices, but I'd like to group them by region.

这是在html中进行选择的示例.

This is an example of select in html.

理想情况下,我想创建一个类似于辅助功能语言选择的下拉列表.

Ideally I'd like to create a dropdown that is similar to accessibility language selection.

有什么方法可以在Interface Builder/swift中复制它吗?任何指针将不胜感激.

Is there any way to replicate this in Interface Builder / swift?Any pointers would be appreciated.

更新:

之所以这样做,是因为我正在向用户显示语音列表.目前,它将所有区域混合在一起,这非常令人困惑.

The reason for this, is because I am displaying a list of speech voices to the user. At the moment, it mixes all the regions together, which is very confusing.

我正在处理一个更新,可以显示英语(英国)",但我想在发布它们之前对其进行分组.

There is an update I'm working on, where I can display "English (United Kingdom)", but I'd like to group them up before releasing it.

推荐答案

以下代码组菜单,但与您提到的方式不同.

The following code groups menu, but not like the way you mentioned.

let items = [["First","Second"],["First","Second"],["First","Second"]]

lazy var addNewViewButton : NSPopUpButton = {
    let popupButton = NSPopUpButton()

    let firstMenuItem = NSMenuItem(title: "First Group", action: nil, keyEquivalent: "")
    let secondMenuItem = NSMenuItem(title: "Second Group", action: nil, keyEquivalent: "")
    let thirdMenuItem = NSMenuItem(title: "Third Group", action: nil, keyEquivalent: "")

    let superMenu = NSMenu()
    superMenu.addItem(firstMenuItem)
    superMenu.addItem(secondMenuItem)
    superMenu.addItem(thirdMenuItem)

    for (index,item) in items.enumerated()
    {
        let menu = NSMenu()
        for title in item
        {
            let menuItem = NSMenuItem(title: title, action: nil, keyEquivalent: "")
            menuItem.target = self
            menu.addItem(menuItem)
        }
        menu.addItem(NSMenuItem.separator())
        superMenu.setSubmenu(menu, for: superMenu.items[index])
    }
    popupButton.menu = superMenu

    popupButton.translatesAutoresizingMaskIntoConstraints = false

    return popupButton
}()

在代码中添加popupbutton,您将获得如下结果

Add the popupbutton in your code and you will get results like this

每个人都将拥有自己的物品.

Each one will be having its own items inside.

这篇关于是否可以在NSPopupButton中对项目进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:24