Swift: Prepare for segue not passing data

女生的网名这么多〃 提交于 2019-12-24 01:53:42

问题


I have got a collection view inside a table view (Netflix App Style). I followed this tutorial: https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/

Now I want to implement a function that whenever I click on a collection view, it will pass the employee's name to the next view controller. Here is my code:

import UIKit

class CategoryRow: UITableViewCell, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    var jobpost:JobPosition?
    var employee: Employee?

    var myname = ""


    func collectionView(_ collectionView: UICollectionView,     numberOfItemsInSection section: Int) -> Int {
        return jobpost!.Employees.count
    }

    func collectionView(_ collectionView: UICollectionView,     cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! EmployeeCell
    cell.employeephoto.image = UIImage(named: "spanner")

    cell.employeename.text = jobpost?.Employees[indexPath.row].name

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let myname = jobpost?.Employees[indexPath.row].name
    //print(myname)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let EmployeeDetailPage = segue.destination as? EmployeeDetailViewController

        EmployeeDetailPage?.employeename = myname
        print(myname)
}

Now my problem is, the whole PrepareForSegue function didn't actually run. The app runs normally without any error but the data just didn't pass through? (i.e. the print(myname) didn't actually print anything).

Is there something I missed out? Very much appreciated.


回答1:


Step 1

Create a global variable in your CategoryRow

var myname: String!

Step 2

Change the didSelectItemAt func like below

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    myname = jobpost?.Employees[indexPath.row].name

    performSegue(withIdentifier: "EmployeeDetailViewController", sender: myname)
}

Step 3

Create a variable in EmployeeDetailViewController

var employeename: String!

Step 4

Give a identifier to your segue. Here I give "EmployeeDetailViewController" Then change the prepareForSegue func like below

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "EmployeeDetailViewController" {

            if let EmployeeDetailVC = segue.destination as? EmployeeDetailViewController {

                EmployeeDetailVC.employeename = myname

            }

        }
}



回答2:


It looks like your problem is that you are setting a local let constant called myname in your collectionView(_:didSelectItemAt:) method instead of setting your class's myname property.

let myname = jobpost?.Employees[indexPath.row].name

should be:

myname = jobpost?.Employees[indexPath.row].name


来源:https://stackoverflow.com/questions/44096117/swift-prepare-for-segue-not-passing-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!