How to use for loop to print array in a page control (swift4)

My code right now uses a for loop to print images placed in the assets folder. I want to do the same thing with text using a label. So in array judo. I would like each entry to be printed in order just like the way it is doing with thee images.

                   @IBOutlet var lz: UILabel!
 var judo = ["a","b","c"]

  override func viewDidLoad() {
super.viewDidLoad()
scrol.delegate = self

for image in 0...2 {
    lz.text = 

    let imageTo = UIImage(named: "\(image).png")
    let imageView = UIImageView(image: imageTo)
    let xCord = view.frame.midX + view.frame.width * CGFloat(image)
    contenetWidth += view.frame.width
    scrol.addSubview(imageView)
}}

@timswift Modify your original code as follows:

@IBOutlet var lz: UILabel!
var judo = ["a","b","c"]
var output = ""

override func viewDidLoad() {
  super.viewDidLoad()
  scrol.delegate = self

  for image in 0...2 {
    output += " \(judo[image])"
    
    let imageTo = UIImage(named: "\(image).png")
    let imageView = UIImageView(image: imageTo)
    let xCord = view.frame.midX + view.frame.width * CGFloat(image)
    contenetWidth += view.frame.width
    scrol.addSubview(imageView)
  }
  
  lz.text = output
}

Please let me know if you have any other questions or issues about the whole thing. Thank you! :]

@shogunkaramazov

What this code is doing is getting a,b,c to appear on the first 3 page control slides. What I want is on slide 1 to read a on slide 2 to read b etc. Just one letter per page. Thanks again for you help in solving this issue.

Hi @timswift,
You have just one Label and if you want that each page has a label, then like you create the UIImageView, create a UILabel and position it on each page inside the loop.

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.