0

I am having trouble passing data (an image and two labels specifically) over from a table view controller to another controller. my goal is to make it so that when a user selects an option from the table view, it will go to the next controller and show the specific image and info for what option they selected. Is there a simple fix that I am just missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ohu
  • 111
  • 1
  • 2
  • 8
  • 1
    Possible duplicate of: [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – TylerP May 17 '20 at 00:39

1 Answers1

0

Create a variable at your destination controller of same type of the variable you want to pass, and use the did select method to navigate to the destination controller and passing the data like below :

func tableView(_ tableView: UITableView, didSelectRowA indexPath: IndexPath) {
    let nextVC = DestinationVC()
    nextVC.info = yourArray[indexPath.row].info
    nextVC.modallyPresentingStyle = .fullscreen
    self.present( nextVC,  animated: true,  completion: nil)
}

Pass all the data you want like info data, don't forget to create all variables at destination controller with same declaration type, for example if you want to pass an image then at your destination VC create an image variable like below

 var selectedImage = UIImage()

And then add it to the previous method

nextVC.selectedImage = yourImage // if not part of a cell at index path
nextVC.selectedImage = yourArray[indexpath.row].yourImage // if it is related to specific cell at index path