最佳答案
过去,NSMatrix
是提供单选组行为的类。如果您查找该类的文档,您会发现以下注释:
Use of NSMatrix is discouraged in apps that run in macOS 10.8 and later. If you need to create a radio button group in an app that runs in macOS 10.8 and later, create instances of NSButton that each specify a button type of NSRadioButton and specify the same action and the same superview for each button in the group.
因此,要实现此目的,需要同时满足三件事:
- 您的所有按钮都必须共享相同的 super View 。
- 所有按钮都必须具有相同的操作。
- 您的按钮需要将其按钮类型设置为 NSRadioButton。
我刚刚尝试过这个。我在 Storyboard的 View 中创建了一个按钮,在属性检查器中将其按钮类型设置为“Radio”,并分配了一个操作。然后我多次重复该按钮,并运行该应用程序。 radio 行为自动起作用。
如果它不适合你,我会从上面做的事情开始。只需创建一个单选按钮并使用操作对其进行设置即可。然后复制几次并测试。如果这有效(应该),那么要么看看你是否可以使用该技术来获得你想要的东西,要么看看你想要的东西和有效的东西之间有什么不同。
关于swift - 将单选组分配给单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61243478/