Search code examples
iosswiftsegue

Can't pass through data to second controller with segue


I can't get a simple segue to work when a cell in a tableview gets pressed. It does go to the next view after I tapped two different items. But I can't pass any values from the first controller to the second. If I set a value to the label in the second controller and load it in the viewDidLoad method it shows up.

I'm going crazy as I've been trying to get this work for ages....

My storyboard: https://snag.gy/DCw9MU.jpg

CategoryListViewController(1st controller):

import Foundation
import UIKit

class CategoryListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var categoryList = TestData.sharedInstance.categoryList


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "iEngineer"
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.tableView .reloadData()
        tableView.dataSource = self
        for category in categoryList{
            print(category)
        }

    }

    // MARK: - Segues
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showFormulaList" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let category = self.categoryList[indexPath.row]
                let formulaListViewController = (segue.destination as! UINavigationController).topViewController as! FormulaListViewController
                formulaListViewController.text = category
                formulaListViewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
                formulaListViewController.navigationItem.leftItemsSupplementBackButton = true
            }
        }

    }

    // MARK: - Table View
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(categoryList.count)
        return categoryList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)
        let object = categoryList[indexPath.row]
        cell.textLabel!.text = object
        return cell
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showFormulaList", sender: self)
    }
}

FormulaListViewController(2nd controller):

import Foundation
import UIKit

class FormulaListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var titleLabel: UILabel!

    var formulaList = TestData.sharedInstance.formulaList

    var fSwift: String!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "iEngineer"
        print(fSwift)
        titleLabel.text = fSwift

    }

    // MARK: - Table View
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(formulaList.count)
        return formulaList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "formulaCell", for: indexPath)
        let object = formulaList[indexPath.row]
        print(object)
        cell.textLabel!.text = object
        return cell
    }
}

Where is my mistake or what am I doing wrong?

I greatly appreciate any help


Solution

  • You need didSelectRowAt instead of didDeselectRowAt

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showFormulaList", sender: self)
    }
    

    Also make sure segue source is the vc not the cell , and since you fire the segue in didDeselectRowAt this

    if let indexPath = tableView.indexPathForSelectedRow 
    

    will be nil