Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

In my app I am queueing some local notifications, when they fire I must present a modal view. The trouble is I have numerous view controllers any one of which could currently be active and thus the one that needs to present the modal view controller. How can I determine which one is currently in use?

I am setting a navigation controller as the windows root view controller, and this can push any number of other view controllers, some of them themselves may also be currently presenting another view controller modally. This must work on iOS 4 and 5.

I have a lot of view controllers so would like to avoid putting code in each of them to each check if they are currently the top one.

like image 263
Gruntcakes Avatar asked Jan 17 '23 06:01

Gruntcakes


1 Answers

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

Since you may also be presenting a modal view controller, you'll probably be more interested in the visibleViewController property, which will give you the controller for the current view whether its presented modally or pushed onto the navigation stack.

like image 155
Caleb Avatar answered Jan 31 '23 00:01

Caleb