How to show different images in uimageview on button click

I've developed an iOS app with a button, segment control and a uiimageview. In the assets folder, there are 26 pictures, named card1 to card26. If I use the below code, it displays each of the 26 images in a random order. How would I flip that code to show each picture in order of image name, card1...card2...etc? I've searched for the last hour and cant find exactly what I'm looking for. I appreciate the help.

let Number = Int.random(in: 2...26) imageView.image = UIImage(named: "card\(Number)")

Accepted Reply

If you want to show the images in order, you need to keep the current number outside of your method.

    var number = 1
    
    func yourMethod() {
        imageView.image = UIImage(named: "card\(number)")
        number += 1
        if number > 26 {number = 1}
    }

Replies

So, you have defined a UIImageView IBOutlet ?


To change the image, just call

        let number = Int.random(in: 2...26)
        let imageName = UIImage(named: "card\(number)")
        imageView.image = UIImage (named: imageName)

I do but leave the int.random will show me the cards in a random order. I want to show them in order of their name (card1, card2, card3...card26)

If you want to show the images in order, you need to keep the current number outside of your method.

    var number = 1
    
    func yourMethod() {
        imageView.image = UIImage(named: "card\(number)")
        number += 1
        if number > 26 {number = 1}
    }

So, what is your spec ? Random or ordered, not the same.

Using a segment control to offer both options.

Then for this case, you have to use random.