gpt4 book ai didi

ios - collectionView 函数 didSelectItemAt indexPath 将数据传递到下一个 View Controller

转载 作者:行者123 更新时间:2023-11-30 11:36:15 25 4
gpt4 key购买 nike

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

我的 Collection View 正确地显示了数据,但是我遇到了 didSelectItemAt 函数的问题...此代码运行良好并且不会引发任何警告或错误,只是变量 myPassVideoCode 未传递到目标 View Controller

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

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 的新本地常量:

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

您只需设置myPassVideoCode。因此更改这一行:

let myPassVideoCode = String(chosenWorkout.videoCode)

至:

myPassVideoCode = String(chosenWorkout.videoCode)

关于ios - collectionView 函数 didSelectItemAt indexPath 将数据传递到下一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49738867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com