Xcode 7: Binding an array controller to a radio button group

血红的双手。 提交于 2019-12-02 14:44:55

问题


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:


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.



来源:https://stackoverflow.com/questions/33450823/xcode-7-binding-an-array-controller-to-a-radio-button-group

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