繁体   English   中英

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

[英]Send Array of images to new view controller

我目前有一个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

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命名约定中所说的一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM