参考UIApplication委托 [英] Getting a reference to the UIApplication delegate
问题描述
AppDelegate
(一个 UIApplicationDelegate
的实例)。我在 applicationDidFinishLaunching
中创建两个实例,并立即显示第一个视图。这工作正常 问题是另一个视图的引用是在 AppDelegate 中,我无法弄清楚如何引用它我可以切换到另一个视图。有没有办法引用主要的 UIApplication
或 UIApplicationDelegate
对象?
是的,UIApplication是一个单例,并使用Objective-C的正常单例模式:
[UIApplication sharedApplication];
您可以直接从您的代表类获取:
MyAppDelegate * delegate =(MyAppDelegate *)[[UIApplication sharedApplication] delegate];
I'm writing my first iPhone application and I'm having trouble switching views. I have 2 views and a reference to each in the AppDelegate
(an instance of UIApplicationDelegate
). I create instances of both in the applicationDidFinishLaunching
and immediately show the first view. This works fine.
The problem is the reference to the other view is in the AppDelegate and I can't figure out how to get a reference to it so I can switch to the other view. Is there a way to get a reference to the main UIApplication
or UIApplicationDelegate
objects?
Yes, UIApplication is a singleton, and uses the normal singleton pattern for Objective-C:
[UIApplication sharedApplication];
You can get your delegate class directly from it:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
这篇关于参考UIApplication委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!