dimanche 5 mars 2017

Need to fix my checkboxes: to change states in one click instead of two clicks. Swift 3, IOS

I have multiple checkboxes that work decently. The way it works is that there's two images (an image of a checked box OR an image of an unchecked box) that show up or disappear into my button, based on clicking that button.

For some reason when it's the first time I click a checkbox it works perfectly (changes its state to: checked or unchecked - when clicked once), but when i go to try a second, third, or fourth (etc.) checkbox, it requires two clicks to change its state (checked/unchecked).

This is annoying and confusing to the user. Is there any way around this?

Here are my last 3 checkboxes:

/////Checkboxes

@IBOutlet weak var Box49: UIButton!

@IBOutlet weak var Box50: UIButton!

@IBOutlet weak var Box51: UIButton!



var BoxON = UIImage(named: "CheckBox")
var BoxOFF = UIImage(named:"UnCheckBox")

var isBoxClicked: Bool!


override func viewDidLoad() {
    super.viewDidLoad()

    isBoxClicked = false
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func Box49(_ sender: Any) {

    if isBoxClicked == true{
        isBoxClicked = false
    }else{
        isBoxClicked = true
    }
    if isBoxClicked == true{
        Box49.setImage(BoxON, for: UIControlState.normal)

    }else{
        Box49.setImage(BoxOFF, for: UIControlState.normal)
    }
}


@IBAction func Box50(_ sender: Any) {

    if isBoxClicked == true{
        isBoxClicked = false
    }else{
        isBoxClicked = true
    }
    if isBoxClicked == true{
        Box50.setImage(BoxON, for: UIControlState.normal)

    }else{
        Box50.setImage(BoxOFF, for: UIControlState.normal)
    }
}


@IBAction func Box51(_ sender: Any) {

    if isBoxClicked == true{
        isBoxClicked = false
    }else{
        isBoxClicked = true
    }
    if isBoxClicked == true{
        Box51.setImage(BoxON, for: UIControlState.normal)

    }else{
        Box51.setImage(BoxOFF, for: UIControlState.normal)
    }
}

Thanks, Dan

Aucun commentaire:

Enregistrer un commentaire