如何获得所有应用程序的ViewControllers在iOS中? [英] How to get all the application's ViewControllers in iOS?

查看:92
本文介绍了如何获得所有应用程序的ViewControllers在iOS中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时获取我的应用程序的所有ViewController?只要我知道 self.navigationcontroller.viewControllers 返回NSArray只有那些控制器在导航堆栈。但是有一些方法我可以访问所有的应用程序的ViewControllers?

解决方案

挂钩Umka的答案。 >

不知道你想做什么,这可能是完全过度的,但它可能会帮助你。



子类的UIViewController(例如AddToArrayViewController),并在自定义init方法添加到一些Singleton你已经定义在某个地方。这样,每当你初始化一个ViewController(它是你的AddToArrayViewController的子类),它将被添加到你的Singleton中的数组。



但是,你需要确保你还可以删除ViewControllers,当他们被删除/清理,否则你只会有一个悬挂指针在该数组中的列表,这是不是真的安全。


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?

解决方案

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.

这篇关于如何获得所有应用程序的ViewControllers在iOS中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆