简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM