3

How can I get all the ViewControllers of my application at runtime? As far as I know self.navigationcontroller.viewControllers returns the NSArray of only those controllers which are on the navigation stack. But is there some way I can access all the my application's ViewControllers?

  • 1
    What is your goal? You can get all view controllers that are currently added. Are you also looking for view controllers used by your app but not yet on display? – rmaddy Feb 07 '13 at 17:57
  • you could use some array to add all the viewcontrollers in it or there is only the way possible like UINavigationController or UITabBarController – nsgulliver Feb 07 '13 at 17:57

2 Answers2

1

AFAIK there is no such way unless you take care your self of it. You could have access to all navigation controllers but they will only hold currently initialised controllers in the stack. There is always controllers possible that are not in any navi, for example, modal view controllers. I would say you are doing something wrong if you need this :)

Perhaps the only way to do this is to hold them all in array that you are fully controlling.

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
1

Hooking in on the answer of Umka.

Without knowing exactly what you want to do, this might be totally overkill, but it might help you.

You could subclass the UIViewController (e.g. the AddToArrayViewController), and in the custom init method add it to some Singleton you have defined somewhere. That way, whenever you initialize a ViewController (which is a subclass of your AddToArrayViewController) it will be added to an array in your Singleton.

BUT, you need to make sure you also remove the ViewControllers when they are removed/cleaned up, otherwise you will just have a list of dangling pointers in that array, which is not really safe.

Wim Haanstra
  • 5,918
  • 5
  • 41
  • 57