Reputation:
This is my working code for buttons in a scrollview that are generated from a for loop.How can i access each of them to change they properties or colors.
@IBOutlet var scrollView: UIScrollView!
var namesOfMuscless = ["ALL","|","CHEST","|","BACK","|","BICEPS","|","TRICEPS","|","DELTOIDS","|","TRAPS","|","ABS","|","OBLIQUES","|","QUADS","|","CALVES","|","INNER THIGHS","|","HAMSTRING","|","GLUTES"]
var nameOfMusclesSizes = [Int]()
var nameOfMusclesSizesFinal = [Int]()
var addFinalSize = 0
var scrollerSize = 0
var pixelAdjustment = 9
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//One letter 20px
for sizeAdd in 0..<self.namesOfMuscless.count {
nameOfMusclesSizes.append(namesOfMuscless[sizeAdd].characters.count)
}
for sizeAddFinal in 0..<self.namesOfMuscless.count {
if(sizeAddFinal<1){
nameOfMusclesSizesFinal.append(0)
}else{
for counter in 0..<self.nameOfMusclesSizesFinal.count {
addFinalSize=nameOfMusclesSizesFinal[counter]
scrollerSize=nameOfMusclesSizesFinal[counter]+13
}
nameOfMusclesSizesFinal.append(nameOfMusclesSizes[sizeAddFinal-1]+addFinalSize)
}
print(nameOfMusclesSizesFinal[sizeAddFinal])
}
scrollView.contentSize = CGSizeMake(CGFloat(scrollerSize*pixelAdjustment-50),200)
scrollView.showsHorizontalScrollIndicator = true
scrollView.indicatorStyle = .Default
for index in 0..<self.namesOfMuscless.count {
let frame1 = CGRect(x:(nameOfMusclesSizesFinal[index]*pixelAdjustment),y: 20, width:namesOfMuscless[index].characters.count*pixelAdjustment, height: 30 )
let button = UIButton(frame: frame1)
button.setTitle(namesOfMuscless[index], forState: .Normal)
button.titleLabel!.font = UIFont(name: "HelveticaNeue", size: 12)
button.backgroundColor = UIColor.blackColor()
button.addTarget(namesOfMuscless[index], action: "buttonClick:", forControlEvents: .TouchUpInside)
scrollView.addSubview(button)
}
}
func buttonClick(sender:UIButton){
if(sender.currentTitle=="|"){
}else{
print(sender.currentTitle)
}
}
How can i change the color of the selected button if it is in a for loop. I have tried it for a couple of hours but nothing worked out. Thanks
Upvotes: 1
Views: 99
Reputation: 23616
You could give each button a tag, and set it to the index of the for loop:
button.tag = index
and then put the button in a Dictionary [Int : UIButton]
for use later
myButtons[index] = button
So, you could create the global variable var myButtons: [Int : UIButton] = [:]
, and then add the following lines in your for loop
button.tag = index
myButtons[index] = button
Then, you can access each button by using its tag, var buttonToEdit: UIButton = myButtons[tagToUse]
.
So, for example, you could store each muscle in a list with its index
var indexes: [String : Int] = [
"ALL" : 0, "CHEST" : 1,
"BACK" : 2, "BICEPS" : 3,
"TRICEPS" : 4,"DELTOIDS" : 5,
"TRAPS" : 6, "ABS" : 7,
"OBLIQUES" : 8, "QUADS" : 9,
"CALVES" : 10, "INNER THIGHS" : 11,
"HAMSTRING" : 12, "GLUTES" : 13
]
And then get a button from the list myButtons
list based on the index stored in indexes
. For example, if you wanted to get the button for ABS, you could use
var absIndex: Int = indexes["ABS"] ?? 0
var absButton: UIButton? = myButtons[absIndex]
Upvotes: 1