Assign Radio Group to Radio Buttons

元气小坏坏 提交于 2021-02-11 13:59:27

问题


I have several radio buttons

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

  • Assigning a single action outlet to all buttons
  • Putting all buttons in a custom view

None of this works.


回答1:


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:

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.

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

  • 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.



来源:https://stackoverflow.com/questions/61243478/assign-radio-group-to-radio-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!