获取对 UIApplication 委托的引用 [英] Getting a reference to the UIApplication delegate

查看:20
本文介绍了获取对 UIApplication 委托的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

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

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屋!

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