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
}
}
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.
Comments