我希望我的收藏视图位于AllWorkoutsVC中,将videoCode字符串数据传递给名为VideoViewVC的下一个viewController。
我的集合视图正确显示数据,但是 didSelectItemAt func 我遇到了问题...此代码运行良好,不会引发警告或错误,只是变量 myPassVideoCode 未传递到目标视图控制器
(为了清楚起见,我删除了一些代码。如果您觉得需要,请要求更多。
集合查看类
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...
您只需要设置我的密码视频代码。所以改变这一行:
let myPassVideoCode = String(chosenWorkout.videoCode)
自:
myPassVideoCode = String(chosenWorkout.videoCode)
你在didSelectItemAt中创建了一个新的常量: let myPassVideoCode = ...
因此,您不会将值存储到在类定义开头声明的变量 myPassVideoCode 中。