将图像数组发送到新的视图控制器

时间:2018-09-17 17:29:43

标签: ios swift uitableview

我目前有一个Tableview,当用户单击时,会将其发送到一个新的ViewController,该ViewController将显示一张图片。但是,由于我需要它们能够滑动以显示与第一张图片关联的第二张图片(取决于所选的行),因此我试图创建一个要发送的数组。我(认为?)已经成功创建了一个数组,但是由于无法正确“发送”代码,我遇到了问题。我想知道哪里错了,需要进行哪些更改才能发送阵列,以便用户可以滑动并查看第二张图片。如果您需要更多代码,请告诉我,我将在其中对其进行编辑。谢谢!

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;

图像视图控制器:

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

1 个答案:

答案 0 :(得分:1)

您的passedImage似乎是UIImage中的imageViewController变量。您需要做的是将其更改为[UIImage]。这将使您存储图像阵列。

之后,您switch看起来像这样。

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.
}

您需要使用其各自的索引访问图像阵列中的图像。

旁注:对变量名使用 lowerCamelCase ,就像在Swift naming conventions中所说的那样。