0

Intro to Issue

So I am developing an app and I need to be able to pass data through to the following view controller. I have my first view controller LocationViewController and then my second view controller TESTLocationItemViewController both are UITableViewControllers and get their data from CoreData. I am able to get the value of the tableViewCell however I then need to pass it through to the following view controller.

Code

So I first define the variable as selectedLocation but I don't assign it a value

class LocationViewController: UITableViewController {
    
    @IBOutlet var LocationTableView: UITableView!
    
    var selectedLocation: String?

Then I have a fun that is triggered when one of the cells is selected which gets the value of the selected cell ready to pass through to the following view controller. After that, I then print it just for testing purposes and then I assign my next view controller the variable of TESTLocationItemVC and then assign a value to my variable in the next view controller by calling TESTLocationItemVC.selectedLocation = currentCellText then I perform a segue to my next view controller. My issue is that it assigns a value but it doesn't get the value in the following viewcontroller.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell
        let currentCellText = currentCell.textLabel?.text
        print(currentCellText!)
        let TESTLocationItemVC = TESTLocationItemViewController()
        TESTLocationItemVC.selectedLocation = currentCellText
        print(TESTLocationItemVC.selectedLocation!)
        let MainappStoryBoard: UIStoryboard = UIStoryboard(name: "Test", bundle: nil)
        let ItemTableviewcontroller = MainappStoryBoard.instantiateViewController(withIdentifier: "ItemViewController")
        self.navigationController?.pushViewController(ItemTableviewcontroller, animated: true)
    }

If anyone has knows what going wrong or any way to fix it any help will be greatly appreciated.

  • Your question is confusing. There are `TESTLocationItemVC` and `ItemTableviewcontroller` which are two different instances. And you are talking about *performing a segue* so creating or instantiating view controllers is the wrong way anyway. And don't get data from a **view** (the cell), get it from the **model** (the data source). And please name variables with starting lowercase letter. – vadian Nov 10 '20 at 10:14

2 Answers2

0

Sounds like you tried to make it work with segues. Which should be just as easy as the didSelectRow solution.

And when it comes to passing data from one view controller to the one which is about to present, you could implement https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepareforsegue.

Mick
  • 954
  • 7
  • 17
0

First, your variable names should start with lowercase characters. Using uppercase makes it harder to distinguish between variables and classes...

Anyway, you create an instance of TESTLocationItemViewController, but you never push this viewcontroller onto the navigation stack in your code. Instead, you instantiate a new viewcontroller here:

let MainappStoryBoard: UIStoryboard = UIStoryboard(name: "Test", bundle: nil)
let ItemTableviewcontroller = MainappStoryBoard.instantiateViewController(withIdentifier: "ItemViewController")

...which you then push onto the navigation stack. So, try pushing TESTLocationItemVC onto the navigation stack instead.

let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)! as UITableViewCell
let currentCellText = currentCell.textLabel?.text
let testLocationItemVC = TESTLocationItemViewController()
testLocationItemVC.selectedLocation = currentCellText
self.navigationController?.pushViewController(testLocationItemVC, animated: true)
rodskagg
  • 3,827
  • 4
  • 27
  • 46
  • By doing that i then gives me this error `Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value` in my `TESTLocationItemViewController` when it goes to register my table view. – GameCrasher545 Nov 10 '20 at 10:20
  • That's probably because you should instantiate it from your storyboard instead of creating an instance of it using TESTLocationItemViewController(). Create it the same way you created your ItemTableviewcontroller instance. – rodskagg Nov 10 '20 at 11:35
  • So i changed my code to this `let TESTLocationItemVC = TESTLocationItemViewController() TESTLocationItemVC.selectedLocation = currentCellText print(TESTLocationItemVC.selectedLocation!) self.navigationController?.pushViewController(TESTLocationItemVC, animated: true)` and that's when it get the issue – GameCrasher545 Nov 12 '20 at 05:47