1

Here I had placed two radio buttons on view controller class but unable to give logic to make a radio button active at a time can anyone help me ?

@IBAction func radioButtonAction(_ sender: KGRadioButton) {
    sender.isSelected = !sender.isSelected

    if sender.isSelected {
        workRadioButton.isSelected = false
    } else{

    }

}
@IBAction func WorkRadiobuttonAction(_ sender: KGRadioButton) {

    sender.isSelected = !sender.isSelected

    if sender.isSelected {
        homeRadioButton.isSelected = false
    } else{

    }
}
Vamsi S
  • 269
  • 3
  • 16
  • Possible duplicate of [How to create radio buttons and checkbox in swift (iOS)?](https://stackoverflow.com/questions/29117759/how-to-create-radio-buttons-and-checkbox-in-swift-ios) – Shamas S Jul 04 '17 at 13:14
  • my question is different @ShamasS – Vamsi S Jul 04 '17 at 13:22
  • Use DLRadioButton instead of KGRadioButton they will provide all things which you need. [DLRadioButton](https://github.com/DavydLiu/DLRadioButton) – Bhavesh Dhaduk Jul 04 '17 at 13:12

1 Answers1

0

I am just using UIButton as radioButton.try like below

Create one array to store radio buttons

var radioButtonArray = [UIButton]()

Declare your radio buttons as UIButton or anyOther

@IBOutlet weak var radioButton1: UIButton!
@IBOutlet weak var radioButton2: UIButton!

Append all buttons into array

 radioButtonArray.append(contentsOf: [radio1,radio2])

Set same target for all buttons

self.radioButton1.addTarget(self, action: #selector(self.folderRadioButtonClicked(_:)), for: UIControlEvents.touchUpInside)
self.radioButton2.addTarget(self, action: #selector(self.folderRadioButtonClicked(_:)), for: UIControlEvents.touchUpInside)

func folderRadioButtonClicked(_ sender:UIButton) {
    for i in 0..<self.radioButtonArray.count {
        if self.radioButtonArray[i] == sender{
            // here you can perform what you want
            self.radioButtonArray[i].setImage(UIImage(named: "checked.png"), for: UIControlState())
        }else {
            self.radioButtonArray[i].setImage(UIImage(named: "unchecked.png"), for: UIControlState())
        }
    }
}
Dharma
  • 3,007
  • 3
  • 23
  • 38