V1P3R
V1P3R

Reputation: 176

How to properly pass UIImage between ViewControllers?

Trying to pass a UIImage between ViewControllers through a UITapGesture. I have added the UITapGesture successfully, however, my segue refuses to perform.

Here is my code in the initial view:

var image: UIImage!

func imageTapped() {

        println("it works!")


         func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

            if (segue == "enlargePhoto") {

                let destinationVC = segue.destinationViewController as! EnlargedPhotoViewController

                destinationVC.bigImage = image


            }else {

                println("your code sucksssss!")
            }

        }
    }

Code for destinationVC:

import UIKit
import Parse

class EnlargedPhotoViewController: UIViewController {

    @IBOutlet var enlargedHomeImage: UIImageView!

    var bigImage: UIImage!


    override func viewDidLoad() {
        super.viewDidLoad()


        self.enlargedHomeImage!.image = self.bigImage

    }

Any idea as to why my UIImage fails to pass? Everything looks fine to me. Thanks!

Upvotes: 0

Views: 901

Answers (1)

Fujia
Fujia

Reputation: 1242

You declared prepareForSegue as a nested function, it is not called by the framework, and that's why you don't get your image passed to the destination. The correct way is to implement prepareForSegue in the class-wise scope (the same scope as your imageTapped. Just put prepareForSegue outside of the curly brace of imageTapped like this:

func imageTapped() {
    // Perform the segue
}

func prepareForSegue() {
// Pass the image reference
}

Upvotes: 1

Related Questions