leaner122
leaner122

Reputation: 657

How to change color of pair of buttons tapped?

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

    }
   }

Upvotes: 0

Views: 150

Answers (5)

bubuxu
bubuxu

Reputation: 2197

Or using $0 to make it shorter for the accepted answer:

allButtons.forEach {
    let color: UIColor = $0.contains(sender) ? .blue : .black
    $0.forEach { $0.tintColor = color }
}

Upvotes: 1

taipingeric
taipingeric

Reputation: 715

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 }

Upvotes: 6

Jay Patel
Jay Patel

Reputation: 2740

There are multiple ways to do that like using tag, array of array of UIButtons, switch case, using stack view, etc. Here an example using tag and outlet collection.

First create outlet collection of outlets.

@IBOutlet var btn1: [UIButton]!
@IBOutlet var btn2: [UIButton]!
@IBOutlet var btn3: [UIButton]!

then

@IBAction func buttonClicked(_ sender: UIButton) {

    if sender.tag == 1 {
        btnClicked(clr1: UIColor.black, clr2: UIColor.blue, clr3: UIColor.blue)
    }
    if sender.tag == 2 {
        btnClicked(clr1: UIColor.blue, clr2: UIColor.black, clr3: UIColor.blue)
    }
    if sender.tag == 3 {
        btnClicked(clr1: UIColor.blue, clr2: UIColor.blue, clr3: UIColor.black)
    }

}

func btnClicked(clr1: UIColor, clr2: UIColor, clr3: UIColor) {
    for i in 0..<2 {
        btn1[i].tintColor = clr1
        btn2[i].tintColor = clr2
        btn3[i].tintColor = clr3
    }
}

Upvotes: 2

Bista
Bista

Reputation: 7893

This can be a shortest format:

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

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

Or use Switch

switch sender {
case one, numOne:
    one.tintColor = .blue
    numOne.tintColor = .blue
case two, numTwo:
    two.tintColor = .blue
    numTwo.tintColor = .blue
default:
    three.tintColor = .blue
    numThree.tintColor = .blue
}

Upvotes: 1

Nirav D
Nirav D

Reputation: 72410

To short your code, first you need to declare two more UIButton instance with your Button outlet, now use these two buttons in your Button action.

var firstSelected = UIButton()
var secondSelected = UIButton()

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

Upvotes: 1

Related Questions