0

I have a tableview, when I click on a cell, I would like to transfer a photo to another ViewController and place it right away in UIImageView

Photo transfer

override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {

    if segue.identifier == "segue" {

        if let detail = segue.destination as? DetailViewController  {

            detail.PhotoProfile = selectedImage

        }
    }
}

on the second view so

@IBOutlet weak var PhotoProfile: UIImageView!

1 Answers1

0

Destination controller's view is not loaded in prepare(for:), It's not the best practice of passing data between controllers but you can make sure that view is loaded before touching any if it's subviews using outlets:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {

    if segue.identifier == "segue" {

        if let detail = segue.destination as? DetailViewController  {
            detail.loadViewIfNeeded()
            detail.PhotoProfile = selectedImage
        }
    }
}
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • 1
    It's bad form to be touching another viewController's outlets. You should keep all control of the outlets in a single viewController. Pass the image to the viewController and let it control its own subviews as shown in the duplicate answer. – vacawama May 25 '19 at 12:19
  • I know. Answer updated mentioning that. Thank's for your care. @vacawama – Mojtaba Hosseini May 25 '19 at 13:29