1

I have 11 Labels and a variable that counts from 1 to 11. If it counts to 5, for example, I want the first 5 Labels to be not hidden and the Labels 6-11 to be hidden. But now I have 11 big if statement and this is not good. But I don't know how to do this. Sadly, I couldn't find answers to my question.

Thanks in advance :)

Mahmut Acar
  • 713
  • 2
  • 7
  • 26
Fabio M.
  • 51
  • 8

1 Answers1

2

First, make sure your labels are in an array. This includes using an outlet collection. See Swift put multiple IBOutlets in an Array for more on that if you don't know how to do it.

Let's say you have your array of labels:

@IBOutlet var labels: [UILabel]!

Now you can create a function that takes a count and updates all of the labels:

func updateLabels(_ count: Int) {
    for index in 0..<labels.count {
        labels[index].isHidden = index >= count
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579