0

I'd like to use the NSTabViewController for switching through 6 different Tabs with the toolbar style.

All tabs have in common that they show different aspects of a Customer entity.

Now I want to add aditional NSToolbarItems to the toolbar of the NSTabViewController? But I haven't found a way to access the toolbar. I also would like to add Space between the ToolbarItems.

Is there a way to do so?

Or how can I add my ViewController from the Storyboard to a NSTabView without using NSTabViewController?

Regards

Oliver


In the meantime I've tried another approach that I thought was more promising but lead to another strange behaviour:

I've created a new NSViewController and put a NSTabView inside. In order to load my already existing ViewControllers I used this

override func viewDidLoad() {
    super.viewDidLoad()
    let customerController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("CustomerVCID")) as! CustomerViewController

    let servicesController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("ServicesVCID")) as! ServicesController

    customerController.customer = self.customer
    servicesController.customer = self.customer

    self.tabView.tabViewItems[0].view = customerController.view
    self.tabView.tabViewItems[1].view = servicesController.view

}

That indeed worked, but now all my NSButtons that have actions will cause my application to crash.

Oliver Koehler
  • 711
  • 2
  • 8
  • 24
  • Just an idea: use `NSTabViewController` without tabs and provide your own toolbar. – Willeke Nov 08 '17 at 23:56
  • You can have the following styles on a NSTabViewController: * Tabs on Top * Tabs on Bottom * Toolbar * Unspecified So how will I be able to have an NSTabViewController without tabs??? – Oliver Koehler Nov 10 '17 at 15:40
  • What does tabstyle Unspecified do? – Willeke Nov 10 '17 at 15:44
  • Same as "Tabs on Top" – Oliver Koehler Nov 10 '17 at 15:45
  • The documentation says about unspecified: "A style that indicates the the tab view controller does not provide the tab selection UI. Your app provides the control (such as an NSSegmentedControl or NSPopUpButton) for navigating between tabs.". – Willeke Nov 10 '17 at 16:00

1 Answers1

1

There is only one toolbar per window. So your NSTabViewController shares it.

  1. Select toolbar mode of NSTabViewController
  2. Override NSWindowController and add your items

Example:

  override func windowDidLoad() {
      super.windowDidLoad()
      window?.toolbar?.insertItem(withItemIdentifier: .print, at: 0)
  }

You can always access your toolbar via following path view->window->toolbar

Your only issue is that there is one delegate per NSToolbar. Which means you have to create your custom NSToolbarItem inside NSTabViewController delegate.

   override func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        if itemIdentifier == .export {
            return ExportToolbarItem.new()
        } else {
            return super.toolbar(toolbar, itemForItemIdentifier: itemIdentifier, willBeInsertedIntoToolbar: flag)
        }
    }

Remember your are required to call super. This is because underlying method wants to create bindings to view controller. In case you need actionable buttons in toolbar just add them without calling super.

Marek H
  • 5,173
  • 3
  • 31
  • 42