参考UIApplication委托 [英] Getting a reference to the UIApplication delegate

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

问题描述

我正在写我的第一个iPhone应用程序,我无法切换视图。我有2个视图,并且引用了 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屋!

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