I have a VC like this
for the checkBoxes i have embedded UIButtons and i am changing their image on click. Here\'s my code
@IBOutlet weak var be
Usama I've implemented a simple button action for all those buttons which may accomplish your need. Here is the step by step implementation -
First, Declare these variables -
var buttonSelected:Bool = false
var selectedButton : UIButton?
Second, Add a single @IBAction for all buttons -
Third, Here is the action method -
@IBAction func btnSelection(_ sender: UIButton) {
if(buttonSelected){
if(selectedButton == sender){
buttonSelected = false
selectedButton = nil
sender.setImage(UIImage(), for: UIControlState.normal)
}
else{
// It's not the previously selected button
//Do your stuff
}
}
else{
buttonSelected = true
selectedButton = sender
sender.setImage(UIImage.init(named: "checkmark"), for: UIControlState.normal)
}
}
Hope it will help you to start with.
Make the Other button disabled in particular scenario. isUserInteractionEnabled = false
You could rename updateCheckImageOnClick(button: UIButton)
to something generic in order to add some logic inside, like:
func update(button: UIButton) {
isBoxClicked = !isBoxClicked
button.setImage(isBoxClicked ? UIImage(named: "checkmark") : UIImage(), for: UIControlState.normal)
if button == bestSellerButton {
trendingButton.isEnabled = false
lowToHighButton.isEnabled = false
highToLowButton.isEnabled = false
} else if button == trendingButton {
bestSellerButton.isEnabled = false
lowToHighButton.isEnabled = false
highToLowButton.isEnabled = false
} else if button == highToLowButton {
trendingButton.isEnabled = false
lowToHighButton.isEnabled = false
bestSellerButton.isEnabled = false
} else {
bestSellerButton.isEnabled = false
trendingButton.isEnabled = false
highToLowButton.isEnabled = false
}
}