Get a reference to the currently visible view controller

ios, uiviewcontroller

Solution

`UINavigationController` has a property called `topViewController`. Maybe it helps you.

Problem

I have a `UIAlertView`. When the user taps a button in the alert view I want to show a new `UIViewController`. In order to achieve this I need to know which view controller is currently visible on screen because that particular view controller is the right one to present the new view controller. The problem is that I have a complex hierarchy of view controllers in my app including a `UINavigationController` and a `UITabBarController` (among others). So I cannot simply use `self.visibleViewController` to get the currently visible view controller. I have found a possible solution on Stackoverflow but I would like to find a neater solution without having to dig through the whole view controller stack.

Original source

Related problems