0

my code is: func PickUpCarCost() {

        imgCheck.image = UIImage(named: "uncheck.png")
        imgUnCheck.image = UIImage(named: "uncheck.png")
        if CarPickUpButton?.tag == 1 {
            imgCheck.image = UIImage(named: "check.png")
           self.pickupChargelabel.text=self.PickUpFare.text!
        }
        else {
            imgUnCheck.image = UIImage(named: "uncheck.png")

        }

}

in this i wants take bool like if button selectes image view image will be check.png if unselect image view image will be uncheck.png how to code for this in swift.

Mannopson
  • 2,634
  • 1
  • 16
  • 32
Ruchi
  • 13
  • 1
  • 5

3 Answers3

1

You can you isSelected variable. And add different image for button state.

imgCheck.setImage(UIImage(named: "uncheck.png"), for: .selected)
imgCheck.setImage(UIImage(named: "check.png"), for: .normal)
0

Add UITapGestureRecognizer on your button like this. or you can use UIButton own property isSelected ?

@IBOutlet weak var myBtn: UIButton!

In your Viewdidload method add these line

let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap")  

myBtn.addGestureRecognizer(tapGesture)

func handleTap(sender: UITapGestureRecognizer) {

  if sender.state == .ended {
      // handling code
  }
  else if sender.state == .began {

  }
}

Add gesture according to your needs.I used long tap and tap gesture

Muhammad Shauket
  • 2,643
  • 19
  • 40
  • sorry, i cant do dis i wants to aply only bool on button. in whchi when uibuttonClick==true{ // imge changed of image view } else if uibuttoclick==fase image reverse – Ruchi Nov 13 '17 at 08:19
  • check the ans of this guy if you dont want to use tap gesture, https://stackoverflow.com/questions/26364869/swift-uibutton-overriding-setselected – Muhammad Shauket Nov 13 '17 at 08:34
0

This code worked for me. Globally define var isclick = false

 func CarDeliverCost() {
        if (!isclick){

            imgUnCheck.image = UIImage(named: "check.png")
            isclick=true



        }
        else
        {
            imgUnCheck.image = UIImage(named: "uncheck.png")
            isclick=false



        }
    }
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
Ruchi
  • 13
  • 1
  • 5