The best way to use checkbox - IOS swift

前端 未结 5 535
谎友^
谎友^ 2021-02-03 16:03

I am going to work on a project that will use a lot of checkboxes. I found a solution like below, but I know this not right way.

 @IBAction func btn_box(sender:          


        
5条回答
  •  广开言路
    2021-02-03 16:51

    Instead of checking each and every button, just use a generic way to handle this. Set all your button to the same IBAction method and implement that like:

    @IBAction func btn_box(sender: UIButton)
    {
        // Instead of specifying each button we are just using the sender (button that invoked) the method
        if (sender.selected == true)
        {
            sender.setBackgroundImage(UIImage(named: "box"), forState: UIControlState.Normal)
            sender.selected = false;
        }
        else
        {
            sender.setBackgroundImage(UIImage(named: "checkBox"), forState: UIControlState.Normal)
            sender.selected = true;
        }
    }
    

提交回复
热议问题