How do I get all apps ViewControllers in iOS?

How can I get all the ViewControllers of my application at runtime? As far as I know, self.navigationcontroller.viewControllers

only returns NSArray of those controllers that are on the navigation stack. But is there a way to access all of the ViewControllers apps?

+3


source to share


2 answers


Interaction with Umka's answer.

Not knowing exactly what you want to do this can be completely overwhelming, but it might help you.



You can subclass UIViewController (like AddToArrayViewController) and in your custom initialization method add it to some kind of Singleton that you defined somewhere. This way, whenever you initialize the ViewController (which is a subclass of your AddToArrayViewController), it will be added to the array in your Singleton.

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

+1


source


AFAIK there is no such way unless you take care of yourself. You can have access to all navigation controllers, but they will only contain the currently initialized controllers on the stack. There are always controllers that are not part of any navi, like modal view controllers. I would say that you are doing something wrong if you need it :)



Perhaps the only way to do this is to keep everything in an array that you have complete control over.

+1


source







All Articles