将单选组分配给单选按钮 [英] Assign Radio Group to Radio Buttons

查看:86
本文介绍了将单选组分配给单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个单选按钮

我需要为其分配一个无线电组,以便一次只能选择一个单选按钮.没有无线电组选项.我尝试过

I need to assign a radio group to it so that only one radio button is selectable at a time.There is no radio group option.I have tried

  • 为所有按钮分配一个动作插座
  • 将所有按钮置于自定义视图中

这些都不起作用.

推荐答案

在过去, NSMatrix 是提供无线电组行为的类.如果您查找该类的文档,则会发现以下注释:

In the old days, NSMatrix was the class that provided the radio group behavior. If you look up the docs for that class, you'll find the following note:

不建议在macOS 10.8及更高版本中运行的应用程序中使用NSMatrix.如果需要在macOS 10.8及更高版本中运行的应用程序中创建单选按钮组,请创建NSButton实例,每个实例指定NSRadioButton的按钮类型,并为该组中的每个按钮指定相同的操作和相同的超级视图.

因此,项都必须同时为真才能生效:

So there are three things that all need to be true at the same time for this to work:

  • 您的所有按钮都必须共享相同的超级视图.
  • 您的所有按钮都必须执行相同的操作.
  • 您的按钮需要将其按钮类型设置为NSRadioButton.
  • Your buttons all have to share the same superview.
  • Your buttons all have to have the same action.
  • Your buttons need to have their button type set to NSRadioButton.

我刚刚尝试了这个.我在情节提要中的视图中创建了一个按钮,在属性"检查器中将其按钮类型设置为单选",并分配了一个动作.然后我重复了几次该按钮,然后运行了该应用程序.无线电行为自动起作用.

I tried this out just now. I created a button in a view in my storyboard, set it's button type to "Radio" in the Attributes inspector, and assigned an action. Then I duplicated that button several times, and ran the app. The radio behavior worked automatically.

如果它不适合您,那么我将从上面做的事情开始.只需创建一个单选按钮并设置一个动作即可.然后重复几次并进行测试.如果可行(应该),那么要么看看您是否可以使用该技术来获得所需的东西,要么查看所需的东西与有效的东西之间的区别.

If it's not working for you, I'd start by doing what I did above. Just create a single radio button and set it up with an action. Then duplicate it a few times and test. If that works (it should), then either see if you can use that technique to get what you want, or look at what's different between what you want and what works.

这篇关于将单选组分配给单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆