How to get all the application's ViewControllers in iOS?

前端 未结 2 2023
情话喂你
情话喂你 2021-01-21 06:27

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 con

2条回答
  •  一个人的身影
    2021-01-21 06:58

    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.

提交回复
热议问题