2014-01-29 150 views
3

在我的項目中,我有一個NSToolBar其中有5 NSToolBarItems。我想刪除最後兩個toolbarItem,其中一個是NSToolbarSpaceItemIdentifier,然後重新加載工具欄。NStoolbar重新加載/刷新

如何做到這一點? 我被困在這個安靜很長一段時間。如果我想從NSToolBar中刪除項目,由於NSToolbarSpaceItemIdentifier,我收到了約束警告。

我想再次調用委託方法,並將最新的items數組傳遞給它。

如何做到這一點?
請幫忙。

回答

2

約束警告似乎是一個單獨的問題 - 我們已經看到這個bug在10.8以下,但不是在10.9以下的同一個項目。
據我可以告訴這是一個框架錯誤,並沒有任何錯誤的使用API​​的相關。

就刪除工具欄項而言:
removeItemAtIndex:是你在找什麼。

像往常一樣,文檔是你的朋友。在這種情況下,請在這裏讀
Toolbar Programming Topics for Cocoa

0

我無法找到一個方法來刷新。不過周杰倫是正確的,他的建議可用於快速復位菜單

//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. 
     */ 
    } 

我覺得這個鏈接是最完整的工具欄的主題。我發現示例代碼特別有用。 「可可的工具欄編程主題」:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Toolbars/Toolbars.html