In0cenT
In0cenT

Reputation: 481

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

Upvotes: 2

Views: 73

Answers (3)

Shehata Gamal
Shehata Gamal

Reputation: 100543

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

Upvotes: 2

Francesco Destino
Francesco Destino

Reputation: 349

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

You should use didSelect instead of didDeselectRowAt, also you should pass something better than self, because with self you are passing the entire CategoryListViewController Try to pass the indexPath like this

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

and change the function prepare in this

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

}

If this doesn't help you try to debug your code and find where you lost your variable :)

Upvotes: 0

DionizB
DionizB

Reputation: 1507

You have used didDeselectRowAt:

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

You need to use didSelectRowAt:

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

Upvotes: 0

Related Questions