How to determine which view controller is currently active / displaying a view?

In my application, I post several local notifications, when they fire, I have to present a modal representation. The problem is that I have numerous view controllers, which of them can be active and, therefore, those that should represent the modal view controller. How to determine which one is currently in use?

I install the navigation controller as the view controller of the Windows root directory, and this can push any number of other view controllers, some of them themselves can also now be another view controller. This should work on iOS 4 and 5.

I have many view controllers, so I would not put code in each of them for every check if they are currently top.

+4
source share
2 answers

You can look at the property of the navigation controller topViewController to find out which controller is on the top of the stack. This will be the one whose view is displayed.

Since you can also present a modal view controller, you are probably more interested in the visibleViewController , which will give you a controller for the current view, whether it will be presented modally or pushed on the navigation stack.

+8
source

Create a variable that stores a pointer to the ViewController that was recently clicked. Each time you click on a new ViewController, update this variable. Then you will always know which one is on top!

0
source

All Articles