Putting an NSPopUpButton in an NSToolbar

cocoa, macos, objective-c, xcode

Solution

It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.

To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.

If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the `toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar` method, you would create a new `NSToolbarItem` per usual, and call `setView:` to assign the custom view to the toolbar item.

Problem

Having wrestled with `NSPopUpButton` in a previous question here I am now trying to place a `NSPopUpButton` inside an `NSToolbar`. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it. I have seen a method that show's a programmatic way of creating an `NSPopUpButton` and then adding it to an `NSToolbar`, but then I can't work out how to do all the Binding stuff that was so handy last time. Interface Builder hasn't been very helpful, so any help gratefully received. P.S. Could I solve this by creating a custom view (containing an `NSPopUpButton` with the usual bindings) and then adding the custom view to the toolbar?

Original source

Related problems