Passing Image to another View Controller (Swift)

In prepare(for:) you can’t access the @IBOutlets of the destination view controller because they haven’t been set up yet. You should assign the image to a property of the destination view controller, and then move it into place in viewDidLoad():

In source view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toBrowsePage" {
        let dvc = segue.destination as! ListPage
        dvc.newImage = postingImage.image
    }
}

In destination view controller:

class ListPage: UIViewController {
    @IBOutlet weak var browsingImage: UIImageView!
    var newImage: UIImage!

    override func viewDidLoad() {
        super.viewDidLoad()
        browsingImage.image = newImage
    }
}

Leave a Comment