How to restrict button click

戏子无情 提交于 2019-12-20 05:38:12

问题


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?


回答1:


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.




回答2:


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




回答3:


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
    }
}


来源:https://stackoverflow.com/questions/46361686/how-to-restrict-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!