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 bestSellerButton: UIButton!
@IBOutlet weak var trendingButton: UIButton!
@IBOutlet weak var lowToHighButton: UIButton!
@IBOutlet weak var highToLowButton: UIButton!
var isBoxClicked = Bool()
func updateCheckImageOnClick(button: UIButton) {
if isBoxClicked == true {
isBoxClicked = false
button.setImage(UIImage.init(named: "checkmark"), for: UIControlState.normal)
}
else {
isBoxClicked = true
button.setImage(UIImage(), for: UIControlState.normal)
}
}
@IBAction func clickedBestSellerBtn(_ sender: Any) {
updateCheckImageOnClick(button: bestSellerButton)
}
@IBAction func clickedTrendingBtn(_ sender: Any) {
updateCheckImageOnClick(button: trendingButton)
}
likewise for the rest two. Now the check mark image is being set properly every time on click gets set and unset, but i want to restrict the user like if he clicks on best seller btn he's unable to choose the rest, likewise if he clicks trending he can't select any other. How would i do that?
You could rename
updateCheckImageOnClick(button: UIButton)
to something generic in order to add some logic inside, like:Make the Other button disabled in particular scenario. isUserInteractionEnabled = false
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 -
Second, Add a single @IBAction for all buttons -
Third, Here is the action method -
Hope it will help you to start with.