How to change color of pair of buttons tapped?

leaner122

I have 3 pairs of buttons named (one,numOne),(two,numTwo),(three,numThree). Initially tintColor of all the buttons is black. I like to change color of pair of buttons to blue if one button in those pair is tapped and back to black when other pair is tapped. I am able to do it by following code but is there any other shortest way than below?

@IBOutlet weak var one: UIButton!
@IBOutlet weak var two: UIButton!
@IBOutlet weak var three: UIButton!
@IBOutlet weak var numOne: UIButton!
@IBOutlet weak var numTwo: UIButton!
@IBOutlet weak var numThree: UIButton!

 @IBAction func buttonTapped(_ sender: UIButton) {
        if sender == one || sender == numOne
        {
            one.tintColor = UIColor.blue
            numOne.tintColor = UIColor.blue
            two.tintColor = UIColor.black
            numTwo.tintColor = UIColor.black
            three.tintColor = UIColor.black
            numThree.tintColor = UIColor.black

    }
   else if sender == two || sender == numTwo
    {
        two.tintColor = UIColor.blue
        numTwo.tintColor = UIColor.blue
        one.tintColor = UIColor.black
        numOne.tintColor = UIColor.black
        three.tintColor = UIColor.black
        numThree.tintColor = UIColor.black

    }
    else
    {
        three.tintColor = UIColor.blue
        numThree.tintColor = UIColor.blue
         two.tintColor = UIColor.black
        numTwo.tintColor = UIColor.black
        one.tintColor = UIColor.black
        numOne.tintColor = UIColor.black

    }
   }
Sai Li
var allButtons: [[UIButton]] = [[one, numOne], [two, numTwo], [three, numThree]]

func tap(_ sender: UIButton) {
    allButtons.forEach { buttons in
        if buttons.contains(sender) {
            buttons.forEach{ $0.tintColor = .blue }
        } else {
            buttons.forEach{ $0.tintColor = .black }
        }
    }
}

shorter one XD

buttons.contains(sender) ? buttons.forEach{ $0.tintColor = .blue } : buttons.forEach{ $0.tintColor = .black }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS - How to change the color of one of a pair of buttons on button click

From Dev

Multiple buttons to single IBAction - How to change background of the previous tapped button?

From Dev

Swift: How to change imageView color to look like it was tapped?

From Dev

How to change the background color of a UIButton when tapped in swift?

From Dev

Swift: How to change imageView color to look like it was tapped?

From Dev

Change color iron-icon when tapped

From Dev

How to change the color of my buttons on Mac OS?

From Dev

Android: how to change color to buttons of AlertDialogFragment

From Dev

How to set rounded buttons with background color and change color on pressed

From Dev

How to set rounded buttons with background color and change color on pressed

From Dev

Change color of window buttons

From Dev

change a buttons frame size when another button tapped Xcode

From Dev

How to change marker icon what it was tapped?

From Dev

How To Change A Sprite Image When Tapped

From Dev

How to change button repeatedly when tapped?

From Dev

Change the color of UILabel inside table cell when cell is tapped

From Dev

Swift - Change color and size of text when UIButton is tapped

From Dev

Change the color of UILabel inside table cell when cell is tapped

From Dev

Swift: Change text color of all UITableViewCells when one cell is tapped

From Dev

How can I change color of background using a group of radio buttons?

From Dev

How to toggle between two buttons and change color using angular

From Java

Change color of png in buttons - ios

From Dev

DialogFragment buttons color change in Lollipop

From Dev

Using buttons to change color in WebGL

From Dev

How to change title text and buttons color WITHOUT changing action overflow menu text color in new Toolbar?

From Dev

How can I extract the background color of a tapped canvas?

From Dev

How to change the location of buttons?

From Dev

How do I change the background image of a UICollectionViewCell that has been tapped?

From Dev

Apple TV – How to only change focus when "Menu" button is tapped

Related Related

  1. 1

    AngularJS - How to change the color of one of a pair of buttons on button click

  2. 2

    Multiple buttons to single IBAction - How to change background of the previous tapped button?

  3. 3

    Swift: How to change imageView color to look like it was tapped?

  4. 4

    How to change the background color of a UIButton when tapped in swift?

  5. 5

    Swift: How to change imageView color to look like it was tapped?

  6. 6

    Change color iron-icon when tapped

  7. 7

    How to change the color of my buttons on Mac OS?

  8. 8

    Android: how to change color to buttons of AlertDialogFragment

  9. 9

    How to set rounded buttons with background color and change color on pressed

  10. 10

    How to set rounded buttons with background color and change color on pressed

  11. 11

    Change color of window buttons

  12. 12

    change a buttons frame size when another button tapped Xcode

  13. 13

    How to change marker icon what it was tapped?

  14. 14

    How To Change A Sprite Image When Tapped

  15. 15

    How to change button repeatedly when tapped?

  16. 16

    Change the color of UILabel inside table cell when cell is tapped

  17. 17

    Swift - Change color and size of text when UIButton is tapped

  18. 18

    Change the color of UILabel inside table cell when cell is tapped

  19. 19

    Swift: Change text color of all UITableViewCells when one cell is tapped

  20. 20

    How can I change color of background using a group of radio buttons?

  21. 21

    How to toggle between two buttons and change color using angular

  22. 22

    Change color of png in buttons - ios

  23. 23

    DialogFragment buttons color change in Lollipop

  24. 24

    Using buttons to change color in WebGL

  25. 25

    How to change title text and buttons color WITHOUT changing action overflow menu text color in new Toolbar?

  26. 26

    How can I extract the background color of a tapped canvas?

  27. 27

    How to change the location of buttons?

  28. 28

    How do I change the background image of a UICollectionViewCell that has been tapped?

  29. 29

    Apple TV – How to only change focus when "Menu" button is tapped

HotTag

Archive