使用App Delegate访问通过Storyboard创建的视图控制器 [英] Accessing a view controller created through Storyboard using the App Delegate
问题描述
我正在使用storyboard开发iOS5应用程序,我在视图控制器类中有一个方法,我想从App Delegate访问。麻烦的是,这个视图控制器通过故事板中的标签栏控制器进行实例化,因此App Delegate无法直接调用我想要的方法...
I'm working on an iOS5 app using storyboard, and I have a method in a view controller class that i'd like to access from the App Delegate. The trouble is, this view controller gets instantiated via a tab bar controller in storyboard, so the App Delegate has no direct way of calling the method I want...
For一个视图控制器与App Delegate联系,所有人必须做的就是使用:
For a view controller to get in touch with the App Delegate, all one has to do is use:
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
是否有类似的简单方法指向已经实例化的视图控制器或类?
Is there a similarly easy way of pointing to an already-instantiated view controller or class?
推荐答案
您必须从应用委托中遍历视图层次结构。假设 AppDelegate
包含对 UITabBarController
的引用,您可以使用 viewControllers
property或 selectedViewController
属性以进入您的视图控制器。
You'll have to traverse the view hierarchy from the app delegate. Assuming the AppDelegate
holds a reference to the UITabBarController
, you could use the viewControllers
property or selectedViewController
property to get to your view controller.
这篇关于使用App Delegate访问通过Storyboard创建的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!