简体   繁体   English

如何将选定的collectionview单元格发送到另一个视图控制器(ios)?

[英]How to send a selected collectionview cell to another view controller (ios)?

I'm being able to detect the selected row (image) in my collection view, But I need to send it to another view controller. 我可以在集合视图中检测到选定的行(图像),但是我需要将其发送到另一个视图控制器。 Here is a part of the code : 这是代码的一部分:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionViewCell {

        cell.cellImage.image = UIImage(named: images[indexPath.row])
        return cell
    } else {
        return CollectionViewCell()

    }
}

//Printinig the selected image ID in console
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    SelectedItem = indexPath.row + 1
    print(SelectedItem)
}

//Navigate to MPViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController = segue.destination as!  MPViewController
    DestViewController.labelText = String(SelectedItem)
}
}

Take one instance variable in your destination class and set value of it in prepare for segue and then in viewDidload set that string to your label's text like, 在目标类中获取一个实例变量,并在准备segue时设置其值,然后在viewDidload将该字符串设置为标签的文本,例如,

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController = segue.destination as!  MPViewController
    DestViewController.yourText = String(SelectedItem) 
 }

ans in viewDidload ans in viewDidload

  yourLabel.text = yourText
//Printinig the selected image ID in console

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    self.SelectedItem = indexPath.row + 1

    self.selectedImage = UIImage(named: images[indexPath.row]);

    print(SelectedItem)
}

//Navigate to MPViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController = segue.destination as!  MPViewController
    DestViewController.imageSelected = self.selectedImage;
    DestViewController.selectedItem = String(self.SelectedItem);

}

Now in MPViewController you can use the data self.imageSelected and self.selectedItem as per your requirements. 现在,在MPViewController中,您可以根据需要使用数据self.imageSelected和self.selectedItem。

Initialize a variable first 首先初始化变量

var imageToPass: UIImage! var imageToPass:UIImage!

Then update didSelectItemAt func 然后更新didSelectItemAt函数

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
    SelectedItem = indexPath.row + 1
    print(SelectedItem)

    self.imageToPass = UIImage(named: images[SelectedItem])

    performSegue(withIdentifier: "TargetVC", sender: imageToPass) //here you give the identifier of target ViewController
}

Go to your TargetVC and initialize a variable 转到您的TargetVC并初始化一个变量

var getImage: UIImage! var getImage:UIImage!

Then override the function in previous VC 然后覆盖以前的VC中的功能

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "TargetVC" {

            if let targetVC = segue.destination as? TargetVC {

                if let imageToPass = sender as? UIImage {

                    TargetVC.getImage = imageToPass

                }

            }

        }
    }

暂无
暂无

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

相关问题 iOS如何将选定的单元格值隔离到另一个视图控制器中 - iOS how to segue a selected cell value into another view controller 如何将选定的tableview单元格文本发送到iOS中的前一个视图控制器,Objective C(向后传递数据) - How to send selected tableview cell text to previous view controller in iOS, Objective C (passing data backward) 如何将选定单元格中的数据传递给另一个视图控制器? - How to pass data in selected cell to another view controller? 选择其他单元格时,取消选择集合视图单元格 - Deselect a collectionview cell when another cell is selected 如何将值从嵌入式collectionView中的选定单元格传递到另一个viewController? - How to pass value from a selected cell in embedded collectionView to another viewController? Swift CollectionView 单元格中的 CollectionView 单元格 - 当前新视图 Controller - Swift CollectionView Cell in a CollectionView Cell - Present New View Controller 通过segue将CollectionView的选定单元格索引发送到viewController - Send selected cell index of CollectionView to viewController by segue 使View动画到collectionView或scrollView中的tableView中的选定单元格 - Animate a View to a selected cell in collectionView or tableView in scrollView 如何将选定的集合视图单元格的图像推送到另一个视图控制器? - How Can I push the image of selected Collection View Cell to another View Controller? 将collectionview插入另一个collection view的单元格中 - Inserting collectionview inside cell of another collection view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM