4

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.

Alexander B
  • 407
  • 2
  • 17

2 Answers2

4

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • It seem like I should roll my own subclass anyway to implement that behaviour, no way achieve the needed behaviour with standard controls... – Alexander B Sep 27 '15 at 07:23
1

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

mangerlahn
  • 4,746
  • 2
  • 26
  • 50
  • This is the most simple solution to your problem. If you want to do anything more special there you have to subclass the segmented control and work from there. – mangerlahn Sep 26 '15 at 16:45