0

I have a small set of objects from which the user should be able to select one using a radio button group. The objects are already bound to an array controller. Is there a way to bind that array controller to a radio button group so that additional radio buttons are generated dynamically? If possible, an IB solution is preferred.

Example project: https://scriptreactor.com/collectionviewtest_radio.zip

Goal is to be able to select one of the items "a", "b", "c", and have that selection available in the view controller.

1 Answer 1

1

You can bind the button state through objectValue (NSTableViewCell) in a view-based table, and representedObject in a collection view (NSCollectionViewItem).

If an NSArrayController is configured to generate cells/items for those parent views, the views will maintain the state of the objectValue and representedObject binding targets for each view in the collection.

So in IB, you have one generic subview with a radio button in it, and you should be able to select "Collection View Item" with a keypath of representedObject.<whateverKeyPath> for the binding of the radio button if it's within a collection view item. Or you could select "Table View Cell" with a keypath of objectValue.<whateverKeyPath> if the button is a subview of a table view cell for a view-based table.

Though it may be dated by recent OSX 10.10 updates to NSCollectionView, the quick start guide has good illustrations on how to configure bindings within subviews of collection views. Table view subview bindings are similar, though you use objectValue rather than representedObject as the property within the view.

5
  • I've built a group with a NSCollectionView, and passed the different radio labels through an ArrayController and your representedObject approach. Now, I have the problem that I still need to bind the actual value (otherwise, selecting the radio buttons doesn't do anything). Additionally, the radio buttons don't behave as a group - i can select all of them at once.
    – Etan
    Nov 1, 2015 at 8:00
  • You can bind the radio button to selected rather than representedObject.<something> and disallow multiple selection. I thought perhaps you were targeting a binding to something in the model that already had a property, and allowing multiple selection. I'm also not sure you need to radio button above and beyond the default selection behavior, but I suppose you can disable that to make it appear as you want. FWIW, the old standard way was the deprecated NSMatrix. It is annoying that they have not spelled out exactly how to replace that behavior.
    – stevesliva
    Nov 2, 2015 at 4:19
  • 1
    I've tried with the NSCollectionView but can't seem to get the binding of the selection done. In the end, I would like to have a single selected object in my ViewController. Example project: scriptreactor.com/collectionviewtest_radio.zip
    – Etan
    Nov 3, 2015 at 13:36
  • @Etan, if you got it working, telling others how to do without NSMatrix would be a great answer of your own here.
    – stevesliva
    Dec 11, 2015 at 19:49
  • Haven't got it working so far. I've returned to using a popup button although they require two clicks from the user...
    – Etan
    Dec 11, 2015 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.