在这里,我在视图控制器类上放置了两个单选按钮,但无法给出逻辑,一次激活一个单选按钮,任何人都可以帮助我吗?
@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{
}
}
答案 0 :(得分:0)
我只是将UIButton用作radioButton.try,如下所示
创建一个数组来存储单选按钮
var radioButtonArray = [UIButton]()
将您的单选按钮声明为UIButton或anyOther
@IBOutlet weak var radioButton1: UIButton!
@IBOutlet weak var radioButton2: UIButton!
将所有按钮附加到数组
radioButtonArray.append(contentsOf: [radio1,radio2])
为所有按钮设置相同的目标
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())
}
}
}