4
votes

I'd like to add a segmented control with menu like in Xcode toolbar to my OS X app. It will switch between three screens and also needs to have some options in each screen in the form of menu.picture of what I need to implement

The standard segmented control does not allow "Select one" mode when I add menu to it.

2

2 Answers

4
votes

This functionality is built in. In code, you can just use the -setMenu:forSegment: method of NSSegmentedControl (or NSSegmentedCell). In Interface Builder, just drag a menu from the Object library and drop it on the appropriate segment.

To allow the segment with a menu to be selected when being clicked, you may need to use a custom subclass of NSSegmentedControl which overrides -startTrackingAt:inView: to set the segment to selected before calling through to super.

1
votes

I was able to accomplish what you were looking for quite easily.

I set the trackingMode of the segmented control to NSSegmentSwitchTrackingMomentary

Then i created an IBAction for the segmented control:

- (IBAction)selectionChanged:(NSSegmentedControl *)sender {
    [NSMenu popUpContextMenu:[NSApp mainMenu] withEvent:[NSApp currentEvent] forView:sender];
}

Then it looks like the following:

enter image description here