collectionView 函数 didSelectItemAt indexPath 将数据传递给下一个视图控制器

Posted

技术标签:

【中文标题】collectionView 函数 didSelectItemAt indexPath 将数据传递给下一个视图控制器【英文标题】:collectionView function didSelectItemAt indexPath to pass data to next view controller 【发布时间】:2018-04-09 17:53:22 【问题描述】:

我希望位于 AllWorkoutsVC 中的 collectionView 将 videoCode 字符串数据传递给名为 VideoViewVC 的下一个 viewController。

我的集合视图正确显示数据,但是 didSelectItemAt 函数我遇到了问题...此代码运行正常,没有引发警告或错误,只是变量 myPassVideoCode 未传递给目标视图控制器

(为了清楚起见,我删除了一些代码。如果您觉得需要,请询问更多。)

collectionView 类

class AllWorkoutsVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource 

      var myPassVideoCode = String()



     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WorkoutsCell", for: indexPath) as? WorkoutsCell 
            let workout = workouts[indexPath.row]
            cell.updateViews(workout: workout)
            print("this is my workouts: " + workout.videoCode)
            return cell
         else 
            return WorkoutsCell()
        
    


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
        let chosenWorkout = DataService.instance.getAllWorkouts()[indexPath.row] 
        myPassVideoCode = String(chosenWorkout.videoCode)
        print("this is my:  " + myPassVideoCode)//Note: this whole print does not appear in the console either
        performSegue(withIdentifier: "onPlayPressed2", sender: chosenWorkout)
    


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
        if segue.identifier == "onPlayPressed2" 
            let toNextVC = segue.destination as! VideoViewVC
            toNextVC.myPassVideoCode = myPassVideoCode
        
    


目标类...

class VideoViewVC: UIViewController, UITableViewDataSource, UITableViewDelegate 

    var myPassVideoCode = String()
    @IBOutlet var videoPlayer: YouTubePlayerView!

    override func viewDidLoad() 
        super.viewDidLoad()
        videoPlayer.loadVideoID(myPassVideoCode)
    

【问题讨论】:

什么问题???? 变量 myPassVideoCode 在传递给目标视图控制器时为 nil 在 VideoViewVC 中声明它像这样 var myPassVideoCode:String? ,,, 也尝试在 didSelect 中打印它?? 很遗憾没有,我还在 didSelectItemAt 函数中添加了 print("this is my videoCode: " + myPassVideoCode) ,结果中没有显示 @Sh_Khan 与 var myPassVideoCode:String?我在 VideoViewVC 中收到错误 ...线程 1:致命错误:在展开可选值时意外发现 nil 【参考方案1】:

问题是您在以下位置声明了一个具有相同名称的新本地常量 myPassVideoCode

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt...

您只需要设置myPassVideoCode。所以改变这一行:

let myPassVideoCode = String(chosenWorkout.videoCode)

到:

myPassVideoCode = String(chosenWorkout.videoCode)

【讨论】:

这个我已经实现了,videoCode仍然没有通过 我看到你的笔记说打印没有出现。选择什么类型的Workout.videoCode。有必要投这个吗?如果是这样,它实际上并没有被投射。您可能需要以不同的方式处理此问题。该行的打印内容是什么样的: print("这是我的锻炼:" +锻炼.videoCode)。如果您能向我们展示所有相关的打印信息可能会有所帮助。 我在 DataService 中有一个包含所有锻炼的数组,锻炼如下 ...Workout(title: "Chest Press", videoCode: "_kAlQ5Bh5aY")... 这个 videoCode 需要传递到 videoPlayer.loadVideoID(HERE) 取决于在 collectionview 中选择了哪个。打印出来的样子也是这样.....这是我的训练:8zqv9dZvxB0?ecver=2(每个单元格重复多次) 但这并不能完全回答我的问题。我假设'videoCode'的类型是一个字符串,因为你在最后一个答案中把它放在引号中,对吗?此外,我发现使用断点比打印所有内容更容易,因为您可以在控制台中执行操作并单步执行您的代码。你试过吗? 对不起,是的 videoCode 是一个字符串,我不是我对断点不太熟悉【参考方案2】:

您正在 didSelectItemAt 中创建一个新常量: let myPassVideoCode = ...

因此,您没有将值存储到您在类定义开始时声明的变量 myPassVideoCode。

【讨论】:

这个我已经实现了,videoCode仍然没有通过

以上是关于collectionView 函数 didSelectItemAt indexPath 将数据传递给下一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从集合视图传递到表视图类?

为啥在 Swift 中 CollectionView 函数被调用了两次?

collectionView 单元格在 tapGestureRecognizer 函数中不起作用

将 collectionview 从函数传递到 Selector (UILongPressGestureRecognizer)

collectionView 函数 didSelectItemAt indexPath 将数据传递给下一个视图控制器

方法没有覆盖超类中的任何函数(覆盖 func collectionView)[重复]