简体   繁体   English

将数据从CollectionView委托传递到另一个ViewController

[英]Pass data from CollectionView Delegate to another ViewController

I'm beginner on Swift. 我是Swift的初学者。

I have CollectionView in my UIViewController and I need to pass data when user clicks on CollectionViewCell to another ViewController . 我的UIViewControllerCollectionView ,当用户单击CollectionViewCell到另一个ViewController时,我需要传递数据。 I tried to override prepareForSegue but I notice that it called first then code on didSelectItemAtIndexPath . 我尝试覆盖prepareForSegue但我注意到它先调用,然后在didSelectItemAtIndexPath上进行编码。

My First ViewController Code: 我的第一个ViewController代码:

import UIKit

class ViewController: UIViewController {

    ...

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController : UICollectionViewDataSource,UICollectionViewDelegate{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collCell", forIndexPath: indexPath) as! MainCollectionViewCell

        ...
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        itemId = items[indexPath.item].id

        let distinationViewController = DistinationViewController()
        distinationViewController.itemId = itemId
    }   
}

My Distination view controller code 我的Distination视图控制器代码

import UIKit

class DistinationViewController: UIViewController{

    var itemId : String!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print(itemId)
    }
}

The value of itemId in DistinationViewController is nil DistinationViewControlleritemIdDistinationViewController nil

Thanks 谢谢

You can try to change the view through code like : 您可以尝试通过以下代码更改视图:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        itemId = items[indexPath.item].id
        let distinationViewController = self.storyboard.instantiateViewControllerWithIdentifier("DestinationViewControllerStoryBoardID") as! DistinationViewController

        distinationViewController.itemId = itemId
self.navigationController.pushViewController(distinationViewController, animated: true)
}

You need to do two things: 您需要做两件事:

  1. Change your func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) to the following one: 将您的func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)更改为以下内容:

      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { // Here you can check which Collection View is triggering the segue if collectionView == collectionView1 { self.performSegueWithIdentifier("fromFirstCollectionView", sender: nil); } else if collectionView == collectionView2 { // from other CollectionView } } 

    and your prepareForSegue method will be like this 您的prepareForSegue方法将如下所示

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Even here you can check for segue.identifiers if you have something to differentiate let dvc = segue.destinationViewController as! DestinationViewController dvc.itemId = "Hello" } 
  2. Connect segue from your View Controller and assign it an identifier 从View Controller连接segue并为其分配identifier

According to your comment you have 3 collectionView so i guess you need 3 segues with 3 different identifiers. 根据您的评论,您有3个collectionView所以我想您需要3个带有3个不同标识符的segue。

Hope this helps! 希望这可以帮助! Happy to discuss if you have more questions. 如果您还有其他问题,很高兴与您讨论。

You can put everything in your prepareForSegue. 您可以将所有内容都放入prepareForSegue中。 Like this: 像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier{
        switch identifier {
            case "distinationViewController":
                if let nextScene =  segue.destinationViewController as? DistinationViewController {
                    if let indexPath = self.collectionView.indexPathsForSelectedItems.lastItem {
                        nextScene.itemId=items[indexPath.item].id
                        nextScene.delegate = self
                }
            }
            default: break
        }
    }
}

暂无
暂无

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

相关问题 将Json响应从一个Viewcontroller传递到另一个Viewcontroller并填充CollectionView - Pass Json response from one Viewcontroller to another Viewcontroller and populate CollectionView 如何将值从嵌入式collectionView中的选定单元格传递到另一个viewController? - How to pass value from a selected cell in embedded collectionView to another viewController? 如何使用委托从 Tableview 单元格内的 Collectionview 单元格导航到另一个 ViewController? - How to Navigate to another ViewController from Collectionview cell which is inside Tableview cell using delegate? 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? 将数据(标签)从TableViewCell传递到另一个ViewController - Pass Data (Label) from TableViewCell to another ViewController 将数据从一个标签栏视图控制器传递到另一标签栏视图控制器 - pass data from one tabbar viewcontroller to another tabbar viewcontroller 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 将数据从一个视图控制器传递到同一视图上的另一个视图控制器 - Pass data to from one viewcontroller to another viewcontroller on the same view Swift 3将数据从一个ViewController传递到另一个ViewController - Swift 3 pass data from one ViewController to another ViewController 将数据从Collectionview传递到UIcollectionResuableview - pass data from Collectionview to UIcollectionResuableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM