I am creating multiple checkBoxes
buttons programatically but I am assigning a single function pressedCheckTickAction
to every created checkBox
button. The task of that function is to check tick
and uncheck tick
the button or you may say the changing of tick images in the button.
The problem I am facing is only last button is changing the shape as "true" means checked tick or "false" means unchecked tick and other previous buttons are calling the function but not responding or changing the tick images unchecked or tick checked.
What can I do to for proper working of all the buttons as I should not create the many functions for many buttons but I want the optimum solution to use this single function pressedCheckTickAction
.
I have researched a lot also This question but I don't like to use this as I am creating buttons programatically and not using interface and I don't have any limit of creating the buttons.
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)")
}
}
}
Here is the out put
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", "", "", ""]
Here is the image of checkbox buttons I have these multiple checkbox buttons, but only last added button works but previously added buttons are not working but just calling the function and changing last button's shape.
CNG index is 3rd and Petrol index is 4th
Aucun commentaire:
Enregistrer un commentaire