Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在我的 OS X 应用程序中添加一个带有 Xcode 工具栏中的菜单的分段控件。它将在三个屏幕之间切换,并且还需要在每个屏幕中以菜单的形式有一些选项。
当我向其添加菜单时,标准分段控件不允许“选择一个”模式。
此功能是内置的。在代码中,您可以只使用(or )的-setMenu:forSegment:方法。在 Interface Builder 中,只需从 Object library 中拖动一个菜单并将其放在适当的段上。NSSegmentedControlNSSegmentedCell
-setMenu:forSegment:
NSSegmentedControl
NSSegmentedCell
要允许在单击时选择带有菜单的段,您可能需要使用NSSegmentedControl覆盖的自定义子类-startTrackingAt:inView:将段设置为选中,然后再调用 super。
-startTrackingAt:inView:
我能够很容易地完成你正在寻找的东西。
我将trackingMode分段控件的设置为NSSegmentSwitchTrackingMomentary
trackingMode
NSSegmentSwitchTrackingMomentary
然后我为分段控件创建了一个 IBAction:
- (IBAction)selectionChanged:(NSSegmentedControl *)sender { [NSMenu popUpContextMenu:[NSApp mainMenu] withEvent:[NSApp currentEvent] forView:sender]; }
然后它看起来像下面这样: