uchicago-mobi / MPCS51030-2017-Winter-Forum

7 stars 0 forks source link

Segue Not Working #155

Closed turabhassan89 closed 7 years ago

turabhassan89 commented 7 years ago

Hi,

When a user taps on an image, I use a Segue to send it to a new view controller. In that view controller I have a Image View which is connected by a name detailImage. I get to the new view controller screen but the image view does not have the image.

Here is my prepare function: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { print("correct") let controller = segue.destination as! imageViewController print(sender!) controller.detailmage?.image = sender as! UIImage?

Here is how I call it: override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let imageToDisplay = allImages[indexPath.row] performSegue(withIdentifier: "showDetail", sender: imageToDisplay) }

Can someone please outline what is going wrong or is this the best way to go about this?

susanstevens commented 7 years ago

The issue you're running into is that your view controller hasn't loaded up it's IBOutlets when prepare(for segue: UIStoryboardSegue, sender: Any?) is called. You should create a property on your view controller that stores the image, which can be set in prepare for segue. Then in viewDidLoad (at which time your image view will have been loaded from storyboard), you can call detailImage?.image = image.