Loc Dai Le
Loc Dai Le

Reputation: 1697

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

    }


}

Upvotes: 1

Views: 1098

Answers (2)

Gyuri T
Gyuri T

Reputation: 290

First of all you have to set the NotCheckMark image for every button, then you can create the same without the boolean flag.

@IBAction func pushAnyButton(sender: AnyObject) {
    if  let  btnImage : UIButton = sender as? UIButton {
        if (btnImage.currentImage == UIImage(named: "CheckMark"))
        {
            btnImage.setImage(UIImage(named: "NotCheckMark"), forState: .Normal)
        }
        else
        {
            btnImage.setImage(UIImage(named: "CheckMark"), forState: .Normal)
        }
    }
}

You can use this function for all the 10 button if you connect this method to the buttons. Or you can this in more compact way like this:

@IBOutlet weak var anyCheckBoxButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    anyCheckBoxButton.setImage(UIImage(named: "CheckMark"), forState: .Selected )
    anyCheckBoxButton.setImage(UIImage(named: "NotCheckMark"), forState: .Normal )
}

@IBAction func pushAnyCheckBoxButton(sender: AnyObject) {
    anyCheckBoxButton.selected = !anyCheckBoxButton.selected
}

The second idea came from here.

Upvotes: 3

Leo Dabus
Leo Dabus

Reputation: 236370

Just give every button you want to use as a check mark a tag = 1000. if sender.tag == 1000 it is off, set it to 1001 and change the image. Try like this:

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func switchImage(sender: UIButton) {
        sender.toggleSwitch
    }
}

extension UIButton {
    var toggleSwitch: Bool {
        if tag == 1000 {
            tag = 1001
            setImage(UIImage(named: "CheckMark"), forState: .Normal)
            return true
        } else {
            tag = 1000
            setImage(UIImage(named: "NotCheckMark"), forState: .Normal)
            return false
        }
    }
}

Upvotes: 0

Related Questions