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.