Devan Devan - 3 months ago 9
Swift Question

Swift: How to Change UIButton Image on clicking submit button

I have four UIButtons which act as checkboxes, on tapping the button, the image changes. I have a submit button, where I perform a certain operation. My question is How do I change the image of the checkbox UIButton to normal and remove the data in textfield on clicking the submit button.

Answer
@IBAction func submitActn(sender : UIButton!){

    checkBoxBtn.imageView?.image = UIImage(named: "image")  //Similar for every checkBtn
    textField1.text = ""
}

Or a much elegant way:-

Go to your Storyboard -> Select your buttons one at a time -> open Their Attributes Inspector ->

For State Config -Default, select a image in image box just below that you want to show as default(uncheck image )

For State Config -Selcted, select a image in image box just below that you want to show as when the button is selected(checked image)

@IBAction func submitActn(sender : UIButton!){

    checkBtn1.selected = false

    checkBtn2.selected = false

    checkBtn3.selected = false

    checkBtn4.selected = fasle

    textField1.text = ""
}
Comments