我正在使用情节提要开发一个 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...
要让视图控制器与 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
属性或 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 创建的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!