I'm trying to dismiss a VC and present a new VC. but I don't want old VC to exist anymore. I use the code below to dismiss current VC and present new one. but this way, there's a time interval between dismiss and present. I don't want the user to notice this. so, I want to present new VC first and then dismiss previous one. Is there any way to do this?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: "login") as! Login
let presentingVC = self.presentingViewController
self.dismiss(animated: false, completion: { () -> Void in
presentingVC!.present(destinationController, animated: true, completion: nil)
})
Disclaimer
So unless you absolutely need to modally present your new VC, then I recommend just performing a segue between the two VCs. It seems that you are only presenting it modally because you want to manually dismiss it later from the original VC. Not only is using a segue this easier in my opinion, but it will also allow you to use the method I've outlined below.
Solution
This likely isn't the most elegant method, but you could pass the instance of the old VC through prepareForSegue
to the next VC, and then dismiss it in the new VC's viewDidLoad
.
For example, in your new VC you could have something like this:
class NewVC: UIViewController {
...
var prevVC: PrevVC!
override func viewDidLoad() {
super.viewDidLoad()
prevVC.dismiss(animated: false, completion: nil)
}
}
So when your newVC loads, it dismisses the previous VC. All you would need to do in your prevVC class is pass on the instance in prepareForSegue
like so.
class PrevVC: UIViewController {
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? NewVC {
destinationVC.prevVC = self
}
}
}
Then of course you would just have to present the newVC when you want and then everything else would be taken care of.