When Actions of NSSegmentedControl in Toolbars Do Not Fire In Their Overflow Menu Item State, Do This
In this fourth and probably still not final part of my series on NSToolbarItems with segmented controls, I just want to share a problem and a quick fix that Nathan Manceaux-Panot brought up today.
Fixing action dispatching within overflow menus (this post)
Nathan recently went through the series to implement segmented controls in toolbars but discovered that the overflow menu items would not enable (a validation problem) and when they enable, they don’t fire the action. When he brought this up today, I investigated.
But some of the menu items were indeed clickable without producing any effect. Uh oh.
The fix is quite simple: make sure you assign the NSToolbarItem’s action. That’s being used when the overflow menu item representations are assembled.
extensionToolbarSegmentedControlSegment{functoolbarItem()->NSToolbarItem{letitem=NSToolbarItem(itemIdentifier:toolbarItemIdentifier)item.label=labelitem.action=action// Make sure to set this!item.menuTitle=menuTitleitem.menuImage=menuImageitem.updateMenuFormRepresentation()returnitem}}
Looking at this code out of context, you might wonder how on earth the toolbar item was supposed to work at all without an action.
It worked because outside its overflow menu item representation, the toolbar item didn’t have to handle clicks anyway. The NSSegmentedControl handled interaction with its segments.