title: Create checkbox button in swift ios programatically
body: <p>I am creating multiple <code>checkBoxes</code> buttons programatically but I am assigning a single function <code>pressedCheckTickAction</code> to every created <code>checkBox</code> button. The task of that function is to <code>check tick</code> and <code>uncheck tick</code> the button or you may say the changing of tick images in the button.</p>
  
  <p>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.</p>
  
  <p>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 <code>pressedCheckTickAction</code>.</p>
  
  <p>I have researched a lot also <a href="https://stackoverflow.com/questions/29117759/how-to-create-radio-buttons-and-checkbox-in-swift-ios">This question</a> 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.</p>
  
  <pre><code>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)")
          }
      }
  }
  </code></pre>
  
  <p>Here is the out put </p>
  
  <pre><code>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", "", "", ""]
  </code></pre>
  
  <p>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.</p>
  
  <p>CNG index is 3rd and Petrol index is 4th</p>
  
  <p><a href="https://i.stack.imgur.com/b8REI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b8REI.png" alt="enter image description here"></a></p>