iosdev
iosdev

Reputation: 125

How to highlight selection only one button at a time from multiple buttons using Swift

In my case, I am trying to creating multiple buttons with single action IBOutlet and tag option. Here, I need to do clicked button selection highlight at a time only one button from multiple buttons. How to achieve this?

My Code

@IBAction private func buttonAction(_ sender: UIButton) {

    if let button = sender as? UIButton {
    // here I need to do selection highlights at a time only one button from four buttons
    }

    // To differentiate different buttons
    switch (sender.tag) {
    case 0:
        print(sender.title(for: .normal)!)
    case 1:
        print(sender.title(for: .normal)!)
    case 2:
        print(sender.title(for: .normal)!)
    case 3:
        print(sender.title(for: .normal)!)
    default:
        print(sender.title(for: .normal)!)
    }
}

Upvotes: 3

Views: 4454

Answers (4)

PGDev
PGDev

Reputation: 24341

Use IBOutletCollection to solve that,

@IBOutlet var buttons: [UIButton]!

@IBAction func buttonAction(_ sender: UIButton) {
    self.buttons.forEach { (button) in
        button.backgroundColor = (button === sender) ? .red : .darkGray
        button.setTitleColor((button === sender) ? .white : .black, for: .normal)
    }
}

Upvotes: 1

Kamran
Kamran

Reputation: 15258

If you know the tags of all the buttons then you can achieve it like this,

@IBAction private func buttonAction(_ sender: UIButton) {
    // Create a list of all tags
    let allButtonTags = [1, 2, 3, 4, 5]
    let currentButtonTag = sender.tag

    allButtonTags.filter { $0 != currentButtonTag }.forEach { tag in
        if let button = self.view.viewWithTag(tag) as? UIButton {
            // Deselect/Disable these buttons
            button.backgroundColor = #colorLiteral(red: 0.80803, green: 0.803803, blue: 0.805803, alpha: 1)
            button.setTitleColor(UIColor.darkGray, for: .normal)
            button.isSelected = false
        }
    }
    // Select/Enable clicked button
    sender.backgroundColor = #colorLiteral(red: 0.1843137255, green: 0.6823529412, blue: 0.9764705882, alpha: 1)
    sender.setTitleColor(UIColor.white, for: .normal)
    sender.isSelected = !sender.isSelected
}

Upvotes: 2

Abhi Yaduwanshi
Abhi Yaduwanshi

Reputation: 410

Create IBOutletCollection

    @IBOutlet var buttons: [UIButton]!

Now change your code on tap of button

    @IBAction func buttonAction(_ sender: UIButton) {
        self.buttons.forEach { (button) in
            //Reset all attibutes of button to default state
            button.backgroundColor = .red
        }
       //highlight sender
       sender.backgroundColor = .green
    }

You can create custom class for button as well

class CustomButton: UIButton {

    override var isHighlighted: Bool {
        didSet {
            backgroundColor = isHighlighted ? .red : .green
        }
    }
}


   @IBOutlet var buttons: [CustomButton]!

   @IBAction func buttonAction(_ sender: CustomButton) {
            self.buttons.forEach { (button) in
                //Reset all attibutes of button to default state
                button.isHighlighted = false
            }
           //highlight sender
           sender.isHighlighted = true
        }

Upvotes: 0

Naresh
Naresh

Reputation: 955

The best way to achieve this to use the outlet collections as:

1) First create the outlet collection & connect with our buttons as:

    @IBOutlet var buttons: [UIButton]!

2) Now in viewDidLoad call the following functions to assign them tags

private func setTags() {

    for (index, button) in buttons.enumerated() {

        button.tag = index
    }
}

3) Now in actions function you can get the indexed button as:

@IBAction func show(sender: UIButton) {

    //Do you work here.
    self.buttons[sender.tag].setTitle("Change Title On tap", for: .normal)
}

Upvotes: 0

Related Questions