Search code examples
swiftxcodeprepare

My prepareForSegue() Method is not working


So I'm trying to move an image from the first page to the second page. But my prepare() method is not working and I do not know why.. I tried to put override before the prepare function but it says the function is not overriding anything so I could not.

import UIKit

class BeginningPage: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate  {

@IBOutlet weak var ImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    // Dismiss the picker if the user canceled.
    dismiss(animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    // The info dictionary may contain multiple representations of the image. You want to use the original.
    guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display the selected image.
    ImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

//MARK: Action

@IBAction func PressImage(_ sender: UITapGestureRecognizer) {
    let imagePickerController = UIImagePickerController()
    imagePickerController.sourceType = .photoLibrary
    imagePickerController.delegate = self
    present(imagePickerController, animated: true, completion: nil)

}

func prepare(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "mySegue" {
    if let destination = segue.destination as? ViewController {
        destination.text = "123"
        destination.image = ImageView.image
        destination.LayerImage.image = ImageView.image
         }
      }
   }
}

Solution

  • Your naming is wrong. Check the right version in docs. Method definition should look like this:

    Swift 3:

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

    Swift 2:

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