我正在以编程方式创建多个checkBoxes
按钮,但我为每个创建的pressedCheckTickAction
按钮分配了一个功能checkBox
。该功能的任务是check tick
和uncheck tick
按钮,或者您可以说更改按钮中的刻度图像。
我面临的问题是只有最后一个按钮将形状更改为“ true”表示选中的勾选或“ false”表示未选中的勾选,而其他先前的按钮正在调用该函数,但没有响应或更改未选中或选中的勾选图像
要使所有按钮正常工作,我应该怎么做,因为我不应该为多个按钮创建许多功能,但我希望使用此单个功能pressedCheckTickAction
的最佳解决方案。
This question我也做了很多研究,但是我不喜欢使用它,因为我是通过编程方式创建按钮而不是使用界面,并且对创建按钮没有任何限制。
override func viewDidLoad() {
super.viewDidLoad()
self.addEmptySubmit()
self.drawWidget()
}
func drawWidget() {
if w.contains(wCheckBox) {
checkButton = UIButton(frame: CGRect(x: 10, y: uniY , width: 30, height: 30))
print("checkBox printed")
checkButton.setBackgroundImage(#imageLiteral(resourceName: "uncheckedblack"), for:.normal)
self.checkButton.tag = i //checkTag
tagCheckinArray.append(i)
checkButton.addTarget(self, action: #selector(pressedCheckTickAction(_:)), for: .touchUpInside)
print("You have a checkin check with tag \(checkButton.tag)")
label = UILabel(frame: CGRect(x: 50, y: uniY-15, width: 200, height: 60))
label.text = lCheckinArray[widgetTagOk]
label.numberOfLines = 4
label.layer.cornerRadius = 3
label.layer.masksToBounds = true
print("You have Label with tag \(label.tag)")
self.checkinScroll.addSubview(checkButton)
self.checkinScroll.addSubview(label)
self.updateScrollY()
uniY += 40
widgetTagOk += 1
}
}
@objc func pressedCheckTickAction(_ sender: UILabel) {
currentTagofCheckbox = checkButton.tag
print("this is the tag of selected checkbox \(String(describing: currentTagofCheckbox))")
checkButton.isSelected = !checkButton.isSelected
let uncheckedValue = "unchecked"
print("\(uncheckedValue)")
if checkButton.isSelected {
checkButton.setBackgroundImage(#imageLiteral(resourceName: "checkedblack"), for: .normal)
getCheckBoxTick = "true"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "true"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
} else {
checkButton.setBackgroundImage(#imageLiteral(resourceName: "uncheckedblack"), for:.normal)
getCheckBoxTick = "false"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "false"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
}
}
func addEmptySubmit() {
if idCheckinArray.isEmpty {
print("sorry idcheckin is empty")
}
else {
for i in idCheckinArray {
print("here is submit array i \(i)")
self.submittedCheckinArray.append("")
print("yes it appends here is submitted rraay in view did load \(submittedCheckinArray)")
}
}
}
这里是输出
this is the submitted array in checkbox tick["", "", "", "", "true", "", "", ""]
this is the tag of selected checkbox Optional(5)
this is the submitted array in checkbox tick["", "", "", "", "false", "", "", ""]
这是复选框按钮的图像,我具有这些多个复选框按钮,但是只有最后添加的按钮有效,但以前添加的按钮不起作用,而只是调用函数并更改了最后一个按钮的形状。
CNG指数排名第三,汽油指数排名第四
答案 0 :(得分:0)
用此替换pushCheckTickAction函数-
@objc func pressedCheckTickAction(_ sender: UIButton) {
currentTagofCheckbox = sender.tag
print("this is the tag of selected checkbox \(String(describing: currentTagofCheckbox))")
sender.isSelected = !sender.isSelected
let uncheckedValue = "unchecked"
print("\(uncheckedValue)")
if sender.isSelected {
sender.setBackgroundImage(#imageLiteral(resourceName: "checkedblack"), for: .normal)
getCheckBoxTick = "true"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "true"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
} else {
sender.setBackgroundImage(#imageLiteral(resourceName: "uncheckedblack"), for:.normal)
getCheckBoxTick = "false"
self.submittedCheckinArray[currentTagofCheckbox!-1] = "false"
print("this is the submitted array in checkbox tick\(submittedCheckinArray)")
}
}