vendredi 27 novembre 2015

The best way to toggle severals UIButtons with same images

In my app I'm using UIButton as check button. I have around 10 UIButton which all should toggle between two Image. An image which shows a "x" and another which shows a check mark. I have an action event for each UIButton, but since all 10 UIButton toggle between two images, is there a way to achieve this the best way. Right now I only know this solution, where I have a Boolean flag for each button and when toggle I set the flag to either true or false. But for me this seems like a bad practice.

Example for one of the UIButton:

var MenuBtnSelect = false
func GlutenSelect(sender: AnyObject)
{
    if(!MenuBtnSelect)
    {
        sender.setImage(UIImage(named: "CheckMark"), forState: .Normal)
        MenuBtnSelect = true
    }
    else
    {
        sender.setImage(UIImage(named: "NotCheckMark"), forState: .Normal)
        MenuBtnSelect = false

    }


}




Aucun commentaire:

Enregistrer un commentaire