AllWorkoutsVC中CollectionView的didSelectItemAt传值至VideoViewVC失败
Hey Marcus, let's troubleshoot why your videoCode isn't getting passed to VideoViewVC when tapping a collection view cell. This is a super common issue, so let's break down the most likely fixes step by step:
1. Verify you're correctly instantiating VideoViewVC (and setting the variable before navigating)
The most frequent mistake here is either misinstantiating the view controller, or setting the variable after pushing/presenting it. Here's how to do it right depending on your setup:
If using Storyboards:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { // 1. Grab the selected videoCode from your data source let selectedVideoCode = yourWorkoutsArray[indexPath.item].videoCode print("Selected code: \(selectedVideoCode)") // Add this to confirm you're getting valid data // 2. Safely instantiate VideoViewVC from your storyboard guard let videoVC = storyboard?.instantiateViewController(withIdentifier: "VideoViewVC") as? VideoViewVC else { print("Failed to load VideoViewVC — check your storyboard identifier!") return } // 3. Set the variable BEFORE navigating videoVC.myPassVideoCode = selectedVideoCode // 4. Navigate to the new VC navigationController?.pushViewController(videoVC, animated: true) }
If using pure code initialization:
Make sure your VideoViewVC has a proper initializer, and you're setting the variable upfront:
// In AllWorkoutsVC's didSelectItemAt let selectedVideoCode = yourWorkoutsArray[indexPath.item].videoCode let videoVC = VideoViewVC() videoVC.myPassVideoCode = selectedVideoCode navigationController?.pushViewController(videoVC, animated: true)
2. Double-check the variable declaration in VideoViewVC
Ensure myPassVideoCode is accessible and mutable — don't mark it as private or let (unless you're using a custom initializer):
class VideoViewVC: UIViewController { // Correct: mutable, accessible from other VCs in the same target var myPassVideoCode: String? override func viewDidLoad() { super.viewDidLoad() // Test if the variable came through if let code = myPassVideoCode { print("Received video code: \(code)") } else { print("No video code received!") } } }
3. If using Storyboard Segues (instead of manual instantiation)
If you're triggering a segue directly from the collection view cell in Storyboard, use prepare(for:sender:) to pass data:
// First, trigger the segue in didSelectItemAt override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { performSegue(withIdentifier: "GoToVideoView", sender: indexPath) } // Then pass data in prepare(for:) override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "GoToVideoView", let videoVC = segue.destination as? VideoViewVC, let indexPath = sender as? IndexPath else { return } videoVC.myPassVideoCode = yourWorkoutsArray[indexPath.item].videoCode }
Just make sure your segue's identifier matches exactly what's in your code, and the segue connects AllWorkoutsVC to VideoViewVC.
Quick Troubleshooting Steps
- Add print statements to confirm
selectedVideoCodehas a valid value before passing it. - Check for typos in
myPassVideoCode(case sensitivity matters in Swift!). - Ensure you're not accidentally creating a new instance of
VideoViewVCafter setting the variable (this is an easy slip-up).
If none of these fix it, feel free to share more code snippets (like your data source setup or VideoViewVC initialization) and we can dig deeper.
内容的提问来源于stack exchange,提问作者Marcus Levi

