2014-01-29 2 views
3

내 프로젝트에서 내가 NSToolBar에 5 NSToolBarItems이 있습니다. 하나가 NSToolbarSpaceItemIdentifier 인 마지막 두 toolbarItems를 제거하고 도구 모음을 다시로드하려고합니다.NStoolbar 다시로드/새로 고침

어떻게 하시겠습니까? 나는이 시점에서 조용하게 오래 머물러있다. NSToolBar에서 항목을 제거하려면 NSToolbarSpaceItemIdentifier으로 인해 제한 경고가 표시됩니다.

위임 메서드를 다시 호출하고 최신 항목 배열을 전달하려고합니다.

어떻게 하시겠습니까?
도와주세요.

답변

2

제약 경고가 별도의 문제인 것으로 나타났습니다. 동일한 프로젝트의 경우 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

관련 문제