objective-c 获取对 UIApplication 委托的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1184146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:58:18  来源:igfitidea点击:

Getting a reference to the UIApplication delegate

iphoneobjective-csingletondelegatesuiapplication

提问by derGral

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 applicationDidFinishLaunchingand immediately show the first view. This works fine.

我正在编写我的第一个 iPhone 应用程序,但在切换视图时遇到了问题。我有 2 个视图和对每个视图的引用AppDelegate(一个实例UIApplicationDelegate)。我在 中创建了两者的实例applicationDidFinishLaunching并立即显示第一个视图。这工作正常。

The problem is the reference to the other view is in the AppDelegateand 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 UIApplicationor UIApplicationDelegateobjects?

问题是对另一个视图的引用在AppDelegate 中,我不知道如何获得对它的引用,以便我可以切换到另一个视图。有没有办法获得对主要UIApplicationUIApplicationDelegate对象的引用?

回答by Louis Gerbarg

Yes, UIApplication is a singleton, and uses the normal singleton pattern for Objective-C:

是的,UIApplication 是一个单例,并且使用 Objective-C 的普通单例模式:

[UIApplication sharedApplication];

You can get your delegate class directly from it:

您可以直接从中获取您的委托类:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

回答by Codebeef

Use:

用:

[[UIApplication sharedApplication] delegate];