Stephan Boner
Stephan Boner

Reputation: 753

Initial View Controller from everywhere

I got an application with a navigation view controller as Initial View Controller. After loading the initial view controller, I set up a notification listener. The Notification can be posted everywhere in the app. I have some pushed vc's and also modally presented. My goal is to return to the initial vc and present a modal view controller from there if the notification is triggered but I have no Idea how to do that. Do I need to do this outside of the MainViewController?

Upvotes: 0

Views: 85

Answers (1)

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

Answer assumes rootViewController is UINavigationController as specified by OP in his question

You can achieve what you want using

(UIApplication.shared.keyWindow?.rootViewController as! UINavigationController).dismiss(animated: true) {
    (UIApplication.shared.keyWindow?.rootViewController as! UINavigationController).popToRootViewController(animated: true)
}

Whats happening is pretty simple. Knowing that your initial viewCOntroller is always UINavigationController, initially check if you have anything presented on rootView controller, if yes dismiss it and in the completion block pop to rootViewController of your initial viewController.

Hope it helps

Upvotes: 1

Related Questions