0

I was able to move from one ViewController to another programmatically without any story board, now come the part where I need to transfer the data, in my rootViewController, I have data in this form:

var restaurants:[Restaurant] = [
          Restaurant(name: "Cafe Deadend",  image: "cafedeadend.jpg", isVisited: false),
          Restaurant(name: "Homei", image: "homei.jpg", isVisited: false) ]

And of course Restaurant is a class in my model section ....

Now in my rootcontroller i move to my other controller like this ......

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let detailView: DetailViewController = DetailViewController()
        
       
         detailView.restaurant = restaurants[indexPath.row]
        self.navigationController?.pushViewController(DetailViewController(), animated: false)
        
        
    }

And of course in my DetailViewController I have this

var restaurant = Restaurant()

But here when I try to show an image like this in DetailViewController, expecting the image name to come with the code in RestaurantViewController I get nil....

let imageHeader = UIImageView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
imageHeader.image = UIImage(named: self.restaurant.image)

I am always getting nil value for the image, where I should be getting the string name of image, can any one please guide where I have gone wrong and why the value is not getting transferred from one controller to other.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
multiverse
  • 425
  • 3
  • 13
  • Does this answer your question? [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Mohmmad S Jun 29 '20 at 09:23
  • `var restaurant = Restaurant()` will overwrite the restaurant you previously set with `detailView.restaurant = restaurants[indexPath.row]`. – Deitsch Jun 29 '20 at 09:24
  • Where have you created `var restaurant = Restaurant()` in DetailViewController? And Is the `DetailViewController` created programatically or using storyboard? – PGDev Jun 29 '20 at 09:32
  • Its a class .swift file separately, programatically, thanks – multiverse Jun 29 '20 at 09:42

2 Answers2

1

Your code has a problem in your didSelectRowAt, since you are not passing the previously created DetailViewController with a restaurant, but another newly-created DetailViewController, try this:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  let detailView: DetailViewController = DetailViewController()
  detailView.restaurant = restaurants[indexPath.row]
  self.navigationController?.pushViewController(detailView, animated: false)
}

See the change on the pushViewController method? This should do it.

hfelpp
  • 403
  • 2
  • 6
0
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let storyboard = UIStoryboard(name: "Main", bundle: nil) // change storyboard name here..
    let detailView = storyboard.instantiateViewController(withIdentifier: 
    "DetailViewController") as! DetailViewController // check View controller identifier
    detailView.restaurant = restaurants[indexPath.row]
    navigationController?.pushViewController(detailView, animated: true)

}