macos - NStoolbar 重新加载/刷新

标签 macos cocoa delegates nstoolbar nstoolbaritem

在我的项目中,我有一个 NSToolBar,它有 5 个 NSToolBarItems。我想删除最后两个工具栏项目,其中一个是 NSToolbarSpaceItemIdentifier,然后再次重新加载工具栏。

如何做到这一点? 我在这一点上沉默了很长时间。如果我想从 NSToolBar 中删除项目,我会因 NSToolbarSpaceItemIdentifier 收到约束警告。

我想再次调用委托(delegate)方法并将最新的项目数组传递给它。

如何做到这一点?
请帮忙。

最佳答案

我找不到刷新的方法。然而 Jay 是正确的,他的建议可以用来快速重置菜单

//Factory Method for reseting toolbar
func setCurrentToolBarItems(desiredItems:[String]){
    // remove all toolbar items
    for _ in (window?.toolbar?.items)!{
        window?.toolbar?.removeItem(at: 0)
    }
    // add new items
    for item in desiredItems{
        window?.toolbar?.insertItem(withItemIdentifier: item, at: 0)
    }
}

下面是一些用于以编程方式创建按钮的附加代码。

import Cocoa


class ToolbarWindowController: NSWindowController, NSToolbarDelegate {

    @IBOutlet var DualToolBar: NSToolbar!

    enum ToolbarItemID : String {
        case DeleteCard = "DeleteSelectedCard", RandomizeCards = "RandomizeCards", CardTest = "CardTest",SwitchFirstCardFaceSeen = "SwitchFirstFace", ExitTest = "ExitTest"

        static let allValues = [DeleteCard, RandomizeCards, CardTest, SwitchFirstCardFaceSeen, ExitTest]
    }


    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    }

    func test() {
        contentViewController?.performSegue(withIdentifier: "goToSlideShow", sender: contentViewController)
        setCurrentToolBarItems(desiredItems: [ToolbarItemID.RandomizeCards.rawValue, ToolbarItemID.SwitchFirstCardFaceSeen.rawValue])

    }

    //Factory Method for reseting toolbar
    func setCurrentToolBarItems(desiredItems:[String]){
        // remove all toolbar items
        for _ in (window?.toolbar?.items)!{
            window?.toolbar?.removeItem(at: 0)
        }
        // add new items
        for item in desiredItems{
            window?.toolbar?.insertItem(withItemIdentifier: item, at: 0)
        }
    }


    // MARK: - NSToolbarDelegate

    func customToolbarItem(itemForItemIdentifier itemIdentifier: String, label: String, paletteLabel: String, toolTip: String, target: AnyObject, itemImage: NSImage, action: Selector?) -> NSToolbarItem? {

        let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)
        toolbarItem.label = label
        toolbarItem.paletteLabel = paletteLabel
        toolbarItem.toolTip = toolTip
        toolbarItem.target = target
        toolbarItem.action = action
        toolbarItem.image = itemImage
        return toolbarItem
    }


    /*
     NSToolbar delegates require this function.
     It takes an identifier, and returns the matching NSToolbarItem. It also takes a parameter telling
     whether this toolbar item is going into an actual toolbar, or whether it's going to be displayed
     in a customization palette.
     */
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: String, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {

        var toolbarItem: NSToolbarItem = NSToolbarItem()

        /* We create a new NSToolbarItem, and then go through the process of setting up its attributes from the master toolbar item matching that identifier in our dictionary of items.
         */

        switch itemIdentifier {
        case ToolbarItemID.CardTest.rawValue:
            let image = NSImage(named: "NSSlideshowTemplate")!
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.CardTest.rawValue, label: "test", paletteLabel: "test", toolTip: "test yourself with this cardset", target: self, itemImage: image, action: #selector(self.test))!
        case ToolbarItemID.RandomizeCards.rawValue:
            let image = NSImage(named: "Randomize Button")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.RandomizeCards.rawValue, label: "randomize", paletteLabel: "randomize", toolTip: "randomize cards so that you are not always tested with the cards in the same order", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.DeleteCard.rawValue:
            let image = NSImage(named: "deleteButton")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.DeleteCard.rawValue, label: "delete", paletteLabel: "delete", toolTip: "delete card on current selected row", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.SwitchFirstCardFaceSeen.rawValue:
            let image = NSImage(named: "Switch")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.SwitchFirstCardFaceSeen.rawValue, label: "switch def/term", paletteLabel: "switch def/term", toolTip: "Allows users to test themselves using either the definition or the term first", target: self, itemImage: image!, action: nil)!
        case ToolbarItemID.ExitTest.rawValue:
            let image = NSImage(named: "Return to editing 2")
            toolbarItem = customToolbarItem(itemForItemIdentifier: ToolbarItemID.ExitTest.rawValue, label: "editor window", paletteLabel: "editor window", toolTip: "Used to exit testing and reenter editing mode", target: self, itemImage: image!, action: nil)!

        default:

            Swift.print("more buttons must be added")
        }


        return toolbarItem
    }

    /*
     NSToolbar delegates require this function.  It returns an array holding identifiers for the default
     set of toolbar items.  It can also be called by the customization palette to display the default toolbar.
     */

    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [String] {
        if contentViewController is CardViewer{
            return [ToolbarItemID.CardTest.rawValue]
        }else{
            return [ToolbarItemID.RandomizeCards.rawValue]
        }

        /*  Note:
         That since our toolbar is defined from Interface Builder, an additional separator and customize
         toolbar items will be automatically added to the "default" list of items.
         */
    }

我认为此链接是工具栏主题最完整的链接。我发现示例代码特别有用。 “cocoa 工具栏编程主题”:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Toolbars/Toolbars.html

关于macos - NStoolbar 重新加载/刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21429021/

相关文章:

macos - NSTextField 在退出第一响应者后绘制白色区域

c# - 无法从 Action<T> 推断出类型参数,但可以从 Func<T> 推断出类型参数

c# - 是 Delegate.CreateDelegate 无法绑定(bind)到结构成员覆盖的错误吗?

ios - Apple Business Custom Apps 需要哪种证书和配置文件

c++ - Leptonica 与 Xcode 框架冲突

iphone - 如何复制 UINavigationBar 渐变颜色?

c# - 作为方法调用参数的函数

macos - 在 objective-c 中编写守护进程

swift - 在 macOS 中以编程方式刷新 DNS

cocoa - 与 NSBrowser 相比,NSBrowserTableView 是什么?