How to use NSToolBar in Xcode 6 and Storyboard?

Blaszard picture Blaszard · Jun 14, 2014 · Viewed 9.6k times · Source

I've been trying to build on a Cocoa app that uses Swift and Storyboard in Xcode 6, but how can I use NSToolbar there?

In Xcode 5 and xib, you can add NSToolbar from within Object Library to any .xib files and then click on the added toolbar to expand it and drag and drop a connection from any items there to a AppDelegate.h file. In this way you can create a IBAction or IBOutlet connection. I confirmed that this can also be done if you use Swift and non-storyboard in Xcode 6. However, it looks like this is not the case in Xcode 6 and Storyboard environment.

I first created a project that uses Storyboard in Xcode 6, but then, I wasn't able to add a NSToolbar from within Object Library to a View Controller in Storyboard. So I added it to Window Controller's Window object in Storyboard. However, in this way I cannot create those connections from any items in the expanded toolbar to either AppDelegate.swift or ViewController.swift.

So my question is:

  • Is it feasible to create a storyboard app that uses NSToolbar?
  • If it is feasible, is the addition of NSToolbar to the Window Controller the proper way to use NSToolBar in Storyboard?
  • Finally, how can I create @IBOutlet and @IBAction connections there?

UPDATE

I found that the accepted answer by @GeorgeVillasboas only works for @IBAction. I am still looking for how to create an @IBOutlet connection...

Answer

George Villasboas picture George Villasboas · Jul 17, 2014

I had this very same problem. The solution works for both Objective-C and Swift projects.

Working with Storyboards on OSX, it creates an instance of NSWindow and segues to another NSViewController as its Window Content Segue, as you described.

On your ViewController, create a standard IBAction to receive the action when the toolbar is clicked. To wire it up with the NSToolbar, just control-drag (or leftClick-drag) from your NSToolbarItem to the FirstResponder object, as shown on the picture below.

Wiring up a NSToolbar on XCode 6

This will open a HUGE list of available connections. Your IBAction will be on that list. Just selected it and you're good to go.

Hope this helps!