- I've got three ViewControllers - V1 V2 and V3.
- A button click on V1, ViewController V2 is displayed with animation FlipHorizontal.
Now on a button click from V2, ViewController should be dismissed back to V1 and display ViewController V3.
I tried this piece of code to achieve this functionality. It's not working though. Please help!
Note : V2 and V3 are in same StoryBoard (Start). V1 is in different story board(main).
var VC3 : ViewController3?
let mainStoryboard: UIStoryboard = UIStoryboard(name: "start",bundle: nil)
self.dismissViewControllerAnimated(false, completion: {
self.VC3 = (mainStoryboard.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3)
self.presentViewController(self.VC3!, animated: true, completion: nil)
})
Update- I get this warning Attempt to present ViewController3 on ViewController2 whose view is not in the window hierarchy!
The error message is quite descriptive:
Lets take a look at your existing code:
In the completion handler, you are trying to present
VC3
fromself
...butself
refers to theViewController
you just dismissed. Thats what that error message is telling you.Instead, you need to present VC3 from the parent of VC2. One way you can achieve that is through the
presentingViewController
property:I replicated your query and created 3 View Controllers: VC1, VC2 and VC3. I achieve this using Delegates and Segues. The Storyboard Setup.
VC1.Swift:
VC2.Swift:
The warning states that "You are presenting ViewController3 on ViewController2, But ViewController2 is not there in the view hierarchy!". This means ViewController2 has dismissed before ViewController3 would be presented. So it is not possible to present ViewController 3 on ViewController2
Use this sample code:
ViewController1
ViewController2
ViewController3
Main.storyboard screenshot:
Start.storyboard:
Please check my GitHub link to test sample project:
https://github.com/k-sathireddy/DismissPresentViewControllers