【问题标题】:Pass data from CollectionView Delegate to another ViewController将数据从 CollectionView Delegate 传递到另一个 ViewController
【发布时间】:2016-07-26 10:57:03
【问题描述】:

我是 Swift 的初学者。

我的UIViewController 中有CollectionView,当用户点击CollectionViewCell 时,我需要将数据传递给另一个ViewController。 我试图覆盖prepareForSegue,但我注意到它首先调用然后在didSelectItemAtIndexPath 上编码。

我的第一个 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
    }   
}

我的 Distination 视图控制器代码

import UIKit

class DistinationViewController: UIViewController{

    var itemId : String!

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

DistinationViewControlleritemId的值为nil

谢谢

【问题讨论】:

  • 你从collectionview单元设置了一个segue?
  • @Keviv 是的,我有
  • 好的,你需要做两件事。 1. 从 ViewController 而不是从单元连接你的 segue。 2. 在collectionView:didSelectItemAtIndexPath 中调用performSegueWithIdentifier 并在那里获取目标视图控制器并在那里分配itemID。
  • 好的,但是我在同一个视图控制器中有 3 个集合视图,我如何确定哪个启动了 segue
  • 请看我的回答,

标签: ios xcode swift uiviewcontroller uicollectionview


【解决方案1】:

你需要做两件事:

  1. 将您的 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
            }
        } 
    

    你的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. 从您的视图控制器连接segue 并为其分配一个identifier

根据您的评论,您有 3 个collectionView,所以我想您需要 3 个带有 3 个不同标识符的 segue。

希望这会有所帮助!如果您有更多问题,很高兴与您讨论。

【讨论】:

    【解决方案2】:

    您可以尝试通过以下代码更改视图:

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

    【讨论】:

    • 这对我有用。但是让主情节提要控制器没有segue是否正确?
    【解决方案3】:

    你可以把所有东西都放在你的 prepareForSegue 中。像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多