Vaishak Lalsangi
Vaishak Lalsangi

Reputation: 37

Present a ViewController, dismiss it, and then segue into another View

I'm trying to do the following:

  1. User presses button
  2. UIImagePickerController pops up
  3. User selects image
  4. UIImagePickerController is dismissed
  5. Segue into another ViewController

As of now, I have it set up that when the user presses a button, a UIImagePickerController is initialized and presented with presentViewController. It is also delegated to the class that this button is linked to as well. After the user picks an image, imagePickerController is called, and dismissViewControllerAnimated is also called. Then, performSegueWithIdentifier is called. However, this last step throws the following warning, and then does not segue:

2016-09-15 03:45:44.870 Food Diary[9941:1540144] Warning: Attempt to present on whose view is not in the window hierarchy!

I am very new to Swift and iOS development, what does this mean and how do I fix it?

Upvotes: 1

Views: 1387

Answers (3)

Kashif
Kashif

Reputation: 4632

It stays on the same ViewController because the UIImagePickerController is still in view hierarchy a the d second ViewController cannot be added on top of UIImagePickerController. To fix, you need to wrap your segue in the completion of UIImagePickerCopntroller dismissal like this:

self.dismiss(animated: true, completion: {
    self.performSegue(withIdentifier: "StartToMain", sender: nil)
})

Upvotes: 0

RakeshDipuna
RakeshDipuna

Reputation: 1610

Try to using these two lines code during presenting and dissmising your view controller

Presenting :

self.view.window.rootviewcontroller.presentViewController("yourVC",animated:true, nil)

Dissmising :

self.view.window.rootViewController.dismissViewController(animated:true completion:nil)

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

Perform segue on completion of dismiss of UIImagePickerController

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    // your code
    self.dismissViewControllerAnimated(true, completion: {
        self.performSegueWithIdentifier("SegueIdentifier", sender: self)
    })
}

Note: With sender I have passed current Controller object you can any object that you want.

Upvotes: 5

Related Questions