0

I have everything working when transitioning from my tableview to my view controller. The only issue I have is when I go from tableview to viewcontrller my variable is not passed. It shows up as blank.

When I print to console in the tableview controller I see that my variable is not blank but when receiving it in the viewcontroller it is blank. I cannot figure out why my variable is not passing to my viewcontroller. Any help or ideas are appreciated.

Tableview Controller method:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Get Cell Label
    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell

    //valueToPass = currentCell.textLabel?.text
    let myVC = storyboard?.instantiateViewController(withIdentifier: "selectedMeterView") as! SelectMeterDetailsViewController
    myVC.stringPassed = (currentCell.textLabel?.text)!
    print(myVC.stringPassed)
    navigationController?.pushViewController(myVC, animated: true)
}

This my viewcontroller to receive the variable:

import UIKit
import Foundation

class SelectMeterDetailsViewController: UIViewController {

    var stringPassed = ""

    @IBOutlet weak var meterNameOuput: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        meterNameOuput.text = "THis is a test \(stringPassed)"
    }


    @IBAction func goBackToTableview(_ sender: UIBarButtonItem) {
        performSegue(withIdentifier: "backToTableVIew", sender: self)
    }
}
bgrow11
  • 31
  • 8
  • Instead of declaring `myVC` in the `didSelectRowAt` method, I would recommend to implement the `prepareForSegue` method for such a process. – Ahmad F Apr 08 '18 at 12:30

1 Answers1

0

First in SelectMeterDetailsViewController declare the var like this

var stringPassed:String?

Second

 @IBAction func goBackToTableview(_ sender: UIBarButtonItem) {
   //// pop the view controller don't segue back
  self.navigationController?.popViewController(animated: true)
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I followed you suggestions and now I am getting nil returning in my label in my second viewcontroller. That is better than before cause I was getting just blank. In my console, i have both a print statement in table view and viewcontroller. nil prints first which is in my second viewcontroller then my tableview controller prints the variable. I would think that tableview would print first then second viewcontroller would print. Any ideas as to why it is doing that? – bgrow11 Apr 08 '18 at 13:07
  • did you set any segues in Interface builder ?? – Shehata Gamal Apr 08 '18 at 13:11
  • Yes, I have a Present Modally going from my table cell to my second viewcontroller. – bgrow11 Apr 08 '18 at 13:14
  • remove that segue – Shehata Gamal Apr 08 '18 at 13:15
  • With that removed when i click a cell it does not load the viewcontroller now. – bgrow11 Apr 08 '18 at 13:44
  • Make sure TableviewController is emebded inside a navigationcontroller print(self.navigationController) inside didSelect – Shehata Gamal Apr 08 '18 at 13:46
  • You should set your label after pushing your controller. – VIP-DEV Apr 08 '18 at 15:04