How to dismiss any popover viewcontrollers when activated in iOS?

157 Views Asked by At

I have some ViewControlls for settings, info etc. Users can close the app settings ViewController open(I mean popover the ViewController). When local notification is received I want the the app go to the root viewcontroller and dismiss any popovers.

1

There are 1 best solutions below

2
On

EDIT this answer is only good if the VC you are trying to go back to does not need any special initialization, since this method creates a new instance of it. keep that in mind.

Try this method, it will remove anything in your stack of View Controllers and make a specific View Controller presented on screen:

func dismissAllAndNavigate(){


    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let window = appDelegate.window
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let rootController = mainStoryboard.instantiateViewController(withIdentifier: "some identifier") as! UIViewController
    window?.rootViewController = rootController

}

Just make sure that the name of the storyboard is correct and the identifier of the view controller in that storyboard is defined.