Search code examples
iosswiftxcodesegue

How To Perform Segue After Picking Image


I would like to perform a segue just after the user chooses an image (by clicking on the image view).

I have already checked the segue id and successfully managed to connect both view controllers.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        // Executed when we select an image from the photo library.
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

            // Sets the image to the image view (not really relevant because
            // we push the user to the DetailVC, but let's leave it).
            self.imageView.image = image

            // Closing the image picker.
            self.dismiss(animated: true, completion: nil)

            // Performing segue (It doesn't perform!).
            print("It gets to this point, but doesn't perform.")
            self.performSegue(withIdentifier: "showDetail", sender: nil)
        }
    }

The program sets the image to the image view, gets to that print statement just after the "perform" function, but the segue is not performed.


Solution

  • in storyboard select the view controller which has the imageview, at the top there is yellowcircle right click drag from there to the view controller you want to segue to and give its identifier an ID = “segueID” then edit your didfinishpicking method like this

    if let img = info[.originalImage] as? uiimage{
    myimageview.image = img
    dismiss(animated: true) {
    self.performSegue(withIdentifier: “segueID”, sender: self)}}