0

I have a VC like this

enter image description here

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?

Usama bin Attique
  • 337
  • 1
  • 3
  • 16

3 Answers3

0

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
    }
}
Phillip
  • 4,276
  • 7
  • 42
  • 74
  • can you please update it for radio buttons like if i select one all the others are unselected? – Usama bin Attique Sep 22 '17 at 11:42
  • Every button should be exclusive? I mean, highToLow button should disable others too? – Phillip Sep 22 '17 at 12:11
  • yeah like if best seller is selected all the other should be deselected and while bestseller being selected if i click at trending then best seller should get deselected. i.e only one at a time. – Usama bin Attique Sep 22 '17 at 12:20
  • as per this initially after segue whatever button i select gets checked and unchecked only and others don't get called at all. maybe cz you haven't enabled them again. so whatever is clicked all the others get disabled. – Usama bin Attique Sep 22 '17 at 13:01
  • how would we enable the other while disabling the currentIndex or the checkmark of whatever cell? – Usama bin Attique Sep 22 '17 at 13:02
0

Make the Other button disabled in particular scenario. isUserInteractionEnabled = false

santhoshkumar
  • 191
  • 2
  • 10
0

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.

Amir Khan
  • 1,318
  • 1
  • 14
  • 39