NSToolbarItemGroup 没有选择,并且不会在单击时发送操作

问题描述 投票:0回答:1

我当前有一个包含 3 个项目的工具栏项目组,但单击任何项目都不会执行任何操作。而且,即使手动设置

NSToolbarItemGroup.selectedIndex
,所有项目也不会突出显示。我缺少什么?在单个项目而不是组上设置
action
属性可以使项目可单击,但仍然没有一个项目被选中。

class ViewController: NSViewController, NSToolbarDelegate {

    let toolbarItemIdentifier = NSToolbarItem.Identifier("group")
    let toolbarItemIdentifierItem1 = NSToolbarItem.Identifier("item1")
    let toolbarItemIdentifierItem2 = NSToolbarItem.Identifier("item2")
    let toolbarItemIdentifierItem3 = NSToolbarItem.Identifier("item3")

    override func viewDidAppear() {
        let toolbar = NSToolbar()
        toolbar.delegate = self
        view.window!.toolbar = toolbar
        view.window!.toolbarStyle = .expanded
    }
    
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return [.flexibleSpace, toolbarItemIdentifier]
    }
    
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return [.flexibleSpace, toolbarItemIdentifier, .flexibleSpace]
    }
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        switch itemIdentifier {
        case toolbarItemIdentifier:
            let item1 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem1)
            item1.image = NSImage(named: NSImage.addTemplateName)!
            item1.label = "add"
            let item2 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem2)
            item2.image = NSImage(named: NSImage.homeTemplateName)!
            item2.label = "home"
            let item3 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem3)
            item3.image = NSImage(named: NSImage.pathTemplateName)!
            item3.label = "path"
            let group = NSToolbarItemGroup(itemIdentifier: itemIdentifier)
            group.subitems = [item1, item2, item3]
            group.selectionMode = .selectOne
            group.selectedIndex = 0
            group.target = self
            group.action = #selector(selectItem(_:))
            return group
        default:
            return nil
        }
    }
    
    @objc func selectItem(_ sender: Any) {
        print(0)
    }

}
cocoa appkit nstoolbar
1个回答
0
投票

为每个工具栏项添加目标和操作,它应该按预期工作。

func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        switch itemIdentifier {
        case toolbarItemIdentifier:
            let item1 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem1)
            item1.image = NSImage(named: NSImage.addTemplateName)!
            item1.label = "add"
            item1.target = self
            item1.action = #selector(selectItem(_:))
            let item2 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem2)
            item2.image = NSImage(named: NSImage.homeTemplateName)!
            item2.label = "home"
            item2.target = self
            item2.action = #selector(selectItem(_:))
            let item3 = NSToolbarItem(itemIdentifier: toolbarItemIdentifierItem3)
            item3.image = NSImage(named: NSImage.pathTemplateName)!
            item3.label = "path"
            item3.target = self
            item3.action = #selector(selectItem(_:))
            let group = NSToolbarItemGroup(itemIdentifier: itemIdentifier)
            group.subitems = [item1, item2, item3]
            group.selectionMode = .selectOne
            group.selectedIndex = 0
            group.target = self
            group.action = #selector(selectItem(_:))
            return group
        default:
            return nil
        }
    }
    
    @objc func selectItem(_ sender: Any) {
        print(sender)
    }
© www.soinside.com 2019 - 2024. All rights reserved.