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
How did you present the view controller? I did the mapping by setting a segue - "show", see the attached screenshot. Try to use modal. If you use push, you should dismiss it with the pop method of the navigation controller.
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.
Any view can read its presentation mode using @Environment(\. presentationMode) , and calling wrappedValue. dismiss() on that will cause the view to be dismissed. The other option is to pass a binding into the view that was shown, so it can changing the binding's value back to false.
Here's a solution for Swift3
To present the ViewController
let NotificationVC = self.storyboard?.instantiateViewController(withIdentifier: "NotificationVC") as! ExecutiveNotificationViewController
self.present(NotificationVC, animated: true, completion: nil)
To dismiss the ViewController:
self.dismiss(animated: true, completion: nil)
You have to get the viewController which presented self (current ViewController). If that view controller is rootViewController, then you can use the code below, if not then query it based on your view controller hierarchy.
if let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "vc3") as? ViewController3 {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController!.present(vc3, animated: true, completion: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With