Home > Enterprise >  Swift: How to change a UIImage with a previous and next button?
Swift: How to change a UIImage with a previous and next button?

Time:11-14

I am a student and new to swift. This assignment requires me to create an app for iOS that shows images along with associated descriptive text. I have 10 images with their own descriptions stored in dictionary. I have a ImageView and textLabel for it. I have a function to display the current image and two button(previous and next) to reveal previous image or next image . But my code has some errors. Please help. Ty.

import UIKit

class ViewController: UIViewController {

    var count = 0

    var photoCollection : [UIImage: String] = [ // store into dictionary
        UIImage(named: "P1")!: "City Tavern Bathroom",
        UIImage(named: "P2")!: "Shafer Trail, Island in the Sky District",
        UIImage(named: "P3")!: "Rivers Bend Group Campground",
        UIImage(named: "P4")!: "Delta at Lake Mead",
        UIImage(named: "P5")!: "Deer between Sequoias",
        UIImage(named: "P6")!: "Arlington House, The Robert E. Lee Memorial",
        UIImage(named: "P7")!: "Brink of the Lower Falls of the Yellowstone River",
        UIImage(named: "P8")!: "Garage Exterior",
        UIImage(named: "P9")!: "DSCF1199",
        UIImage(named: "P10")!: "The Bi-national Formation"
    ]

    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var Text: UILabel!

    func showImage() {
        if count < photoCollection.count {
            if let images = photoCollection[count] as? Dictionary<UIImage, String> {
                photo.image = images.keys.first
                Text.text = images.values.first
            }
        } else {
            debugPrint("Failed!")
        }
    }

    @IBAction func Previous(_ sender: UIButton) {
        count = count - 1
        showImage()
    }


    @IBAction func Next(_ sender: UIButton) {
        count = count   1
        showImage()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

CodePudding user response:

A dictionary is not the right tool for the job, but if you must use a dictionary, you should at least ensure that the key is a bit more user friendly than a UIImage. If you define a type to hold the image and the text, you can use an integer to key into the dictionary to access the values you need.

class ViewController1: UIViewController {

    struct Photo {
        let image: UIImage?
        let text: String
    }

    var count = 0

    var photoCollection: [Int: Photo] = [
        0: Photo(image: UIImage(named: "P1"), text: "City Tavern Bathroom"),
        1: Photo(image: UIImage(named: "P2"), text: "Shafer Trail, Island in the Sky District"),
        2: Photo(image: UIImage(named: "P3"), text: "Rivers Bend Group Campground"),
        3: Photo(image: UIImage(named: "P4"), text: "Delta at Lake Mead"),
        4: Photo(image: UIImage(named: "P5"), text: "Deer between Sequoias"),
        5: Photo(image: UIImage(named: "P6"), text: "Arlington House, The Robert E. Lee Memorial"),
        6: Photo(image: UIImage(named: "P7"), text: "Brink of the Lower Falls of the Yellowstone River"),
        7: Photo(image: UIImage(named: "P8"), text: "Garage Exterior"),
        8: Photo(image: UIImage(named: "P9"), text: "DSCF1199"),
        9: Photo(image: UIImage(named: "P10"), text: "The Bi-national Formation"),
    ]

    func showImage() {
        guard let item = photoCollection[count] else { return }
        photo.image = item.image
        Text.text = item.text
    }
}

CodePudding user response:

I would use an array of dictionaries:

import UIKit

class ViewController: UIViewController {
    var count = 0

    var photoCollection: [[String:Any]] = [
        ["image": UIImage(named: "P1")!, "text": "City Tavern Bathroom"],
        // Other photos
    ]

    @IBOutlet weak var photo: UIImageView!
    @IBOutlet weak var Text: UILabel!

    func showImage() {
        photo.image = photoCollection[count]["image"]
        Text.text = photoCollection[count]["text"]
    }

    @IBAction func Previous(_ sender: UIButton) {
        guard count > 0 else {return}
        count -= 1
        showImage()
    }

    @IBAction func Next(_ sender: UIButton) {
        guard count < photoCollection.count - 1 else {return}
        count  = 1
        showImage()
    }
}

You could also disable the buttons when there is no the possibility to go backward or forward.

  • Related