Ron Pelayo Ron Pelayo - 1 year ago 622
Swift Question

Dismiss and Present View Controller in Swift

Hi I'm trying to present a viewcontroller and dismiss my current modal view but this code is not working

self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("OrderViewController")
self.presentViewController(vc!, animated: true, completion: nil)

vice versa is not working too on completion block of presentviewcontroller

EDIT: replaced vc! to self

Answer Source

You have to get the viewController which presented the self (current ViewController). If that view controller is rootViewController that you can use as below, if not that query it based on you view controller hierarchy.

if let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("vc3") as? ViewController3 {
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                appDelegate.window?.rootViewController!.presentViewController(vc3, animated: true, completion: nil)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download