该应用从电子商务应用的API产品选项(如颜色和大小)加载。现在,我有点陷入代码中。
如果选择了一个按钮让我们说出它的颜色,则应该取消选择另一个按钮,因为您一次只能有一种颜色。请看下面的代码。下面的代码是点击按钮时的目标操作。
func imgSelected(_ sender: RadioButton) {
guard let currentButton = sender as? UIButton else { return }
if ((currentButton.isSelected) != nil){
currentButton.isSelected = true
var dict = JSON(self.catalogProductViewModel.getOption[(sender.superview?.tag)!]);
let productOptionArray : JSON = JSON(dict["product_option_value"].arrayObject!)
imageId[(sender.superview?.tag)!] = productOptionArray[sender.tag]["product_option_value_id"].stringValue
currentButton.layer.borderWidth = 3.0
currentButton.layer.borderColor = UIColor.red.cgColor
print("Button Not Clicked \((sender as? RadioButton)?.tag)")
} else {
currentButton.layer.borderWidth = 0
currentButton.layer.borderColor = UIColor.clear.cgColor
print("Button Removed \((sender as? RadioButton)?.tag)")
}
}
然而,我可以看到所有选项都是可选择的我在论坛中尝试了所有可能的例子。在将产品选项投射到购物车时我面临的另一个问题是它返回选项标题而不是选定的值,例如它正在转换为“颜色”而不是“红色”。
else if dict["type"].stringValue == "image" {
if dict["required"].intValue == 1{
if imageId[i] == ""{
isValid = 1;
errorMessage = errorMessage+dict["name"].stringValue
print("Error Message", errorMessage)
}else{
optionDictionary[dict["product_option_id"].stringValue] = imageId[i] as AnyObject
print("Else is Valid 0", optionDictionary[dict["product_option_id"].stringValue] )
}
}else{
optionDictionary[dict["product_option_id"].stringValue] = imageId[i] as AnyObject
print("Stand Alone", optionDictionary[dict["product_option_id"].stringValue])
}
}
对于像效果这样的单选按钮,您可以执行以下操作
//This is the implementation of my custom button
class RadioButton: UIButton {
override var isSelected: Bool {
didSet {
refresh()
}
}
private func refresh() {
//Here We will do when button state changed to selected (maybe radion image selected/ unselected)
if isSelected {
//do the selection
layer.borderWidth = 1.0
layer.borderColor = UIColor.red.cgColor
} else {
//clear the selection
layer.borderWidth = 0.0
layer.borderColor = UIColor.clear.cgColor
}
}
}
class MyViewController: UIViewController {
@IBOutlet var radioButtons: [RadioButton]!
//let say when buttons is clicked we get the call to this function'
@IBAction private func radioButtonTapped(_ sender: RadioButton) {
//first clear the buttons selected state
clearAllSelection()
//now select the one that triggered this function
sender.isSelected = true
}
//clears selected state for all buttons
private func clearAllSelection() {
radioButtons.forEach {
$0.isSelected = false
}
}
}
好的,所以最后我有时间查看RadioButton图书馆,我想你可能忘了将按钮分组,以便它们都属于同一组,因此只有一个将从该组中选择。我使用Library createButtonMethod
创建了一个小样本。请检查并告诉我如果这就是你所追求的。
func createButtons() {
let xpos: CGFloat = 50
var ypos: CGFloat = 100
for i in 1...3 {
//create the button with frame
let frame = CGRect(x: xpos, y: ypos, width: 60, height: 50) //frame for the button
let radioButton = RadioButton(frame: frame)
radioButton.backgroundColor = UIColor.red
//append that button
buttonArray.append(radioButton)
//increase the ypos
ypos += 65
//set the tag
radioButton.tag = i
//add the target
radioButton.addTarget(self, action: #selector(radioButtonSelected(_:)), for: .touchUpInside)
//set the selected and unselected state image of radio button
radioButton.setImage(#imageLiteral(resourceName: "checked"), for: .selected)
radioButton.setImage(#imageLiteral(resourceName: "unchecked"), for: .normal)
//finally add that button to the view
view.addSubview(radioButton)
}
//set the group
buttonArray.first!.groupButtons = buttonArray
//set first one slected
buttonArray.first!.isSelected = true
debugPrint(buttonArray)
}
@objc func radioButtonSelected(_ sender: RadioButton) {
debugPrint("TAG : \(sender.tag)")
}
这里buttonArray
是变量var buttonArray = [RadioButton]()