2

I currently have the following function inside of my main view controller (View Controller A).

func myAction() {       
    let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewControllerC : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerC") as UIViewController

    self.present(viewControllerC, animated: false, completion: nil)
}

When View Controller A is currently displayed and myAction is called it works fine and displays viewControllerC.

But this function myAction can be called basically at any time. Sometimes even when View Controller A is not the current view controller on the screen. Sometimes when View Controller B is displayed this function still gets called. It gets called fine. But it doesn't load viewControllerC in that case.

I've also tried the following thinking that would display from whatever the active view controller is. But that didn't work either.

self.view.window?.rootViewController?.present(viewControllerC, animated: false, completion: nil)

How can I get it to display viewControllerC when myAction is called no matter what view controller is currently being displayed?

Nishant Bhindi
  • 2,242
  • 8
  • 21
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • 3
    A lot of ideas here about getting the top view controller (http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller) – danh May 17 '17 at 00:38
  • @danh Perfect! I used the accepted answer and that seems to work! Thank you very much. – Charlie Fish May 17 '17 at 00:44

2 Answers2

1

You presented one View Controller already and want to present second View Controller from presented View Controller, for this purpose you must call this:

UIApplication.shared.keyWindow?.rootViewController?.presentedViewController?.present(newViewController, animated: true, completion: nil)

Yigit Yilmaz
  • 359
  • 4
  • 12
0

you need to get the top viewController that are currently visible in application for getting top viewController Use this code

extension UIApplication {
    class func topView(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navController = controller as? UINavigationController {
            return topViewController(controller: navController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }
}
Optimus
  • 800
  • 5
  • 16
  • you can create a newClass name UIApplication+Extension and put the code in it.. you can call that method throughout the application. Eg. UIApplication.topView() – Optimus May 17 '17 at 06:35
  • Does that answer resolve your problem with efficient way ? – Optimus May 17 '17 at 12:54
  • I haven't gotten a chance to test this. I've been using the accepted answer from (http://stackoverflow.com/questions/26667009/get-top-most-uiviewcontroller). I plan to test this out soon and if it works I'll be sure to up vote and accept, since it seems like a better solution then the other solution I'm using now. – Charlie Fish May 17 '17 at 20:51