beans217
beans217

Reputation: 139

Send Array of images to new view controller

I currently have a Tableview that when the user clicks, will send them to a new viewcontroller that will display one picture. However, because I need them to be able to swipe to display a second picture associated with the first (depending on which row selected), I am trying to create an array to be sent. I (think?) have successfully created an array, but I'm running into problems with the code as it will not "send" correctly. I am wondering where I am wrong, and what changes I need to make to be able to send the array, so the user can swipe and see the second picture. If you need more code, please let me know and I will edit it with it in there. Thank you!

let firstchoice: [UIImage] = [
    UIImage(named: "Appa1")!,
    UIImage(named: "Appa2")!
    ]


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    ///Right way here
    ///You can easily manage using this
    let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageViewController") as! imageViewController



    ///Here you have written four Animal names in Array
    ///So There is going to four case 0,1,2,3 and a default case
    switch indexPath.row
    {
    case 0:
        Vc.passedImage = UIImage.init(named: firstchoice)
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 1:
        Vc.passedImage = UIImage.init(named: "AppA2")!
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 2:
        Vc.passedImage = UIImage.init(named: "AppB")!
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 3:
        Vc.passedImage = UIImage.init(named: "AppC")!
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 4:
        Vc.passedImage = UIImage.init(named: "AppD")!
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 5:
        Vc.passedImage = UIImage.init(named: "AppE")!
        self.navigationController?.pushViewController(Vc, animated: true)
        break;

Image View Controller:

import UIKit
import GoogleMobileAds


class imageViewController: UIViewController,GADBannerViewDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate {

    var bannerView: GADBannerView!

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var myImageView: UIImageView!


    //var passedImage : UIImage! = nil
    var passedImage : [UIImage]


    override func viewDidLoad(){
        super.viewDidLoad()
        self.myImageView.image = passedImage
        self.navigationController?.navigationBar.isHidden = false

        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 5.0

Upvotes: 0

Views: 815

Answers (1)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

Your passedImage seems to be a UIImage variable in imageViewController. What you need to do is change that to [UIImage]. This will let you store an array of images.

After which you switch would look like this.

switch indexPath.row {
    case 0:
        Vc.passedImage = firstchoice
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 1:
        Vc.passedImage = [UIImage.init(named: "AppA2")!]
        self.navigationController?.pushViewController(Vc, animated: true)
        break;
    case 2:
    // Rest of your cases follow suit.
}

You would need to access an image from the image array using their respective index.

Sidenote: Use lowerCamelCase for variable names like they say in the Swift naming conventions.

Upvotes: 1

Related Questions