How to restrict button click

前端 未结 3 1087
花落未央
花落未央 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.

提交回复
热议问题