How to restrict button click

前端 未结 3 1088
花落未央
花落未央 2021-01-27 18:10

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         


        
相关标签:
3条回答
  • 2021-01-27 18:23

    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.

    0 讨论(0)
  • 2021-01-27 18:35

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

    0 讨论(0)
  • 2021-01-27 18:49

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题