[Solved] Swift: How to make sure that the button returns image to previous state [closed]


You can keep one extra Boolean variable to maintain the state of the button. Modify the bool value on every click. On didSet of the variable, you can update your button Image. If you have multiple states of the button you can have one enum.

private var isButtonActive: Bool = true {
    didSet {
        let image = isButtonActive ? "Image1": "Image2"
        button.setImage(image, for: .normal)
    }
}

func buttonClick() {
    isButtonActive = !isButtonActive
}

1

solved Swift: How to make sure that the button returns image to previous state [closed]