How to Create Nsradiobutton Group in Xcode 7 Osx

Group of NSButton as radio button's group

The button is in the view hierarchy, so it can ask for its superview. Then it can iterate the superviews subviews, look for NSButton of the type radio button and compare action selector to build a group.

Assign Radio Group to Radio Buttons

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.

How to create radio buttons and checkbox in swift (iOS)?

For Radio Buttons and CheckBoxes there is nothing that comes built in.

You can implement Checkboxes easily yourself. You can set an uncheckedImage for your button for UIControlStateNormal and a checkedImage for your UIControlStateSelected. Now on tap, the button will change its image and alternate between checked and unchecked image.

To use radio buttons, you have to keep an Array for all the buttons that you want to behave as radio buttons. Whenever a button is pressed, you need to uncheck all other buttons in the array.

For radio buttons you can use SSRadioButtonsController
You can create a controller object and add buttons array to it like

var radioButtonController = SSRadioButtonsController()
radioButtonController.setButtonsArray([button1!,button2!,button3!])

The main principle is something like this here.

How can I implement a radio button in OSX, Xcode 8, Swift 3?

It looks like there might be a bug in the IDE — it gets confused if the action has a parameter of type Any. You can work around it by modifying the action method to accept a NSButton sender explicitly:

demo gif

NSButton Push On Push Off Type

What you're looking for is called state. You can compare whether the button in the following way:

Objective-C

self.playButton.state == NSControlStateValueOn  // or NSControlStateValueOff

Swift

self.playButton.state == .on  // or .off

Enabled is for whether you can interact with the control at all, a disabled control won't receive user input.

Wrap NSButton title

I don't believe you can. You'd have to subclass NSButtonCell to add support for this.

That said, it's typically a bad idea to have multiple lines of text on a button. A button label should concisely represent the action performed:

The label on a push button should be a verb or verb phrase that describes the action it performs—Save, Close, Print, Delete, Change Password, and so on. If a push button acts on a single setting, label the button as specifically as possible; “Choose Picture…,” for example, is more helpful than “Choose…” Because buttons initiate an immediate action, it shouldn’t be necessary to use “now” (Scan Now, for example) in the label.

What are you trying to do?



Related Topics



Leave a reply



Submit