title: How to add a custom NSToolbarItem to an existing toolbar programmatically
body: <p>I am having difficult to add a custom NSToolbarItem to my existing toolbar.</p>
  
  <p>NSToolbar was created in NSWindowController, then I have a function to populate toolbar items programmatically, code as:</p>
  
  <pre><code>public func populateFileToolbarItem(_ toolbar: NSToolbar) -&gt; Void{
      let itemId = NSToolbarItem.Identifier("FILE_OPEN")
      let index = toolbar.items.count
      var toolbarItem: NSToolbarItem
      toolbarItem = NSToolbarItem(itemIdentifier: itemId)
      toolbarItem.label = String("File")
      toolbarItem.paletteLabel = String("Open File")
      toolbarItem.toolTip = String("Open file to be handled")
      toolbarItem.tag = index
      toolbarItem.target = self
      toolbarItem.isEnabled = true
      toolbarItem.action = #selector(browseFile)
      toolbarItem.image = NSImage.init(named:NSImage.folderName)
      toolbar.insertItem(withItemIdentifier: itemId, at: index)
  }
  </code></pre>
  
  <p>Then I called this function to add the toolbar item to an existing toolbar in windowController</p>
  
  <pre><code>.......
    populateFileToolbarItem((self.window?.toolbar)!)
    self.window?.toolbar?.insertItem(withItemIdentifier: NSToolbarItem.Identifier.flexibleSpace, at: (self.window?.toolbar?.items.count)!)
    self.window?.toolbar?.insertItem(withItemIdentifier: NSToolbarItem.Identifier.print, at: (self.window?.toolbar?.items.count)!)
    print("after toolbaritems were inserted into toolbar. \(String(describing: self.window?.toolbar?.items.count))") 
  ......
  </code></pre>
  
  <p>The console print out shows, there are only two toolbar items were added to toolbar.</p>
  
  <pre><code>.......
  after toolbaritems were inserted into toolbar. Optional(2)
  </code></pre>
  
  <p>And there is no custom item shows in the toolbar.</p>
  
  <p>Any one has experience, please advise!</p>