Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swift中的segue将信息传递给新的视图控制器_Swift_Xcode - Fatal编程技术网

如何使用swift中的segue将信息传递给新的视图控制器

如何使用swift中的segue将信息传递给新的视图控制器,swift,xcode,Swift,Xcode,我在做一个简单的项目时遇到了一个问题。这个应用程序基本上只是显示一组图像,当我点击其中一个图像时,它会转换到一个新的视图控制器,并在标签上显示图像的名称 我是斯威夫特的新手。这是否与我在函数中用于填充集合视图的indexPath有关?我试图查看是否可以获取indexPath,然后将其传递到images数组中,以获取名称并在第二个视图控制器中发送名称,但结果为零。任何帮助都将不胜感激 这是我的CollectionViewController class ImagePickerViewCo

我在做一个简单的项目时遇到了一个问题。这个应用程序基本上只是显示一组图像,当我点击其中一个图像时,它会转换到一个新的视图控制器,并在标签上显示图像的名称

我是斯威夫特的新手。这是否与我在函数中用于填充集合视图的indexPath有关?我试图查看是否可以获取indexPath,然后将其传递到images数组中,以获取名称并在第二个视图控制器中发送名称,但结果为零。任何帮助都将不胜感激

这是我的CollectionViewController


    class ImagePickerViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    @IBOutlet var collectionView: UICollectionView!

    var images = Data()

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell {
            cell.imageView.image = UIImage(named: "\(images.images[indexPath.item])")
            return cell
        }
        return UICollectionViewCell()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let dest = segue.destination as? FeedPickerViewController{
            if segue.identifier == "detailViewController_segue"{
                let cell = sender as! CollectionViewCell
                let indexPath = self.collectionView.indexPath(for: cell)
                let imgName = images.images[indexPath!.item]
                dest.imgName = imgName
            }
        } 
    }
}
这就是我正在使用的ViewController


class DetailViewController: UIViewController {
    var imgName: String!
    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = "Posting Image: \(imgName)"
    }
}
这是数据类

class Data {

    let images = ["bear", "bird", "bridge", 
                  "cabin"]
}

您应该记住indexPath,它是所选单元格的

您可以使用
collectionView:didSelectItemAtIndexPath:
检索所选单元格的索引

selectedIndexPath=indexPath

然后使用选定的IndexPath将选定对象的数据传递给目标视图控制器

dest.imageName=images.images[selectedIndexPath!.item]
试试这个

class ImagePickerViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView!

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return DataImage.images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell {
            cell.imageView.image = UIImage(named: DataImage.images[indexPath.item])
            return cell
        }
        return UICollectionViewCell()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let dest = segue.destination as? FeedPickerViewController{
            if segue.identifier == "detailViewController_segue"{
                let cell = sender as! CollectionViewCell
                let indexPath = self.collectionView.indexPath(for: cell)
                let imgName = DataImage.images[indexPath!.item]
                dest.imgName = imgName
            }
        } 
    }
}
  • 使用struct而不是Class

首先,您必须将类数据名称更改为另一个名称,因为数据是数据对象的保留关键字!。您的代码意味着segue是从collection view单元格连接的,而不是从controller连接的。我按照您的建议实现了
collectionView:didSelectItemAtIndexPath:
,但是当我运行调试器时,我注意到这个函数根本没有被调用,我选择的路径变量是nil,它进入segue函数并出现在新的view controller中。是否因为单元格中的
imageView
?应用程序正在图像而不是单元格上注册点击?请确保在调用
didSelectItemAtIndexPath:
后调用segue(
prepare
class DetailViewController: UIViewController {
    var imgName: String!

    @IBOutlet var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = "Posting Image: " + imgName
    }
}
struct DataImage {

    static var images = ["bear", "bird", "bridge", "cabin"]
}