lundi 30 avril 2018

Indexes or tags? For multiple checkboxes as buttons in IBOutletCollection - Swift 4 IOS

I'm kind of new to swift, but someone mentioned that I should use indexes or tags for the buttons in an IBOutletCollection.

Im not sure how to index or tag specific buttons, and then reference them in the cases:

// images of checked and unchecked boxes for the button to switch between
var BoxON = UIImage(named: "CheckBox")
var BoxOFF = UIImage(named:"UnCheckBox")

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    boxes.forEach {
        $0.setImage(BoxOFF, for: .normal)
        $0.setImage(BoxON, for: .selected)
    }

}

@IBOutlet var boxes : [UIButton]!

@IBAction func boxTouched(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
    let index = boxes.index(of: sender)!
    switch index {

        case 0:
            sender.isSelected = !sender.isSelected
        case 1:
            sender.isSelected = !sender.isSelected
        case 2:
            sender.isSelected = !sender.isSelected
        case 3:
            sender.isSelected = !sender.isSelected
        case 4:
            sender.isSelected = !sender.isSelected
        default:
            sender.isSelected = !sender.isSelected

    }
}

Thanks in advance! :)




Aucun commentaire:

Enregistrer un commentaire