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

时间:2018-04-09 17:53:22

标签: ios swift uicollectionview

我希望我的collectionView位于AllWorkoutsVC中,将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)
    }

2 个答案:

答案 0 :(得分:0)

问题是您在 myPassVideoCode 中声明了一个名为 myPassVideoCode 的新本地常量:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.Count = 1 Then

        If Not Intersect(Target, Range("K11:L20")) Is Nothing Then

            Select Case Target
            Case "UK"
                CountryVariable = "H:AG"
            Case "Brazil"
                CountryVariable = "AI:AZ"
            Case "India"
                CountryVariable = "BB:CB"
            Case Is = "Indonesia"
                CountryVariable = "CD:DH"
            Case "Turkey"
                CountryVariable = "DJ:EE"
            End Select

            Sheets("Team").Visible = True
            Sheets("Country").Visible = False
            Sheets("Team").Activate
            Sheets("Team").Range("K9").Select
        End If
    End If
End Sub

Private Sub Worksheet_Activate()
    Call ResetButton1
    Call StartButton1
    ActiveSheet.Columns("H:FA").EntireColumn.Hidden = True
    On Error Resume Next
    ActiveSheet.Columns(CountryVariable).EntireColumn.Hidden = False
    On Error GoTo 0
    ActiveSheet.Range("G9").Select
    ActiveWindow.Zoom = 80
End Sub

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

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

为:

let myPassVideoCode = String(chosenWorkout.videoCode)

答案 1 :(得分:0)

你在didSelectItemAt中创建一个新的常量: let myPassVideoCode = ...

因此,您不会将值存储到您在类定义开头声明的变量myPassVideoCode。