0

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?

Stephan Boner
  • 733
  • 1
  • 6
  • 27
  • Keep a reference of your NavigationController in the AppDelegate and observe the notifiction in your AppDelegate. When you get a call, go to rootController of navigationController – SWAT Mar 18 '18 at 15:48
  • So your app only has one navigation controller and when the notification is posted, you want the user to be returned back to the root view controller, no matter where they are, and then modally present a view controller on top of the root? – trndjc Mar 18 '18 at 15:49
  • https://stackoverflow.com/questions/47322379/swift-how-to-dismiss-all-of-view-controllers-to-go-back-to-root/47322464 Here you find an answer – yerpy Mar 18 '18 at 15:50

1 Answers1

1

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

Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78