最佳应用代表实践 [英] Best Application Delegate Practice
问题描述
我一直在这里和那里制作一些应用程序,而且我知道自己的出路.一直让我感到困惑的是访问全局属性,以及设置它们的最佳位置.我对访问事物以及如何访问它们有一些疑问.
I have been making a few apps here and there, and I know my way around. What has always confused me is accessing global attributes, and where the best place to set them are. I have a few questions about accessing things and how to access them.
您是否必须将应用程序委托头文件包含在您要访问的其他文件中?假设我有一个要使用的视图控制器,是否需要在视图控制器的.h中包含.h?或者我可以设置:
Do you have to include your application delegates header file into any other other file you want to access it in? Say I have a view controller, that I would like to use, do I need to include the .h inside my view controller's .h? Or can I set the:
@class AppDelegate;
您只能通过键入以下内容来访问委托人吗:
Can you only access the delegate by typing out:
[UIApplication sharedApplication].delegate
每次?那是我只需要习惯的东西吗?还是可以在每个.h的实现中设置以下内容:
EACH and every time? Is that something I just have to get used to? Or could I set the following in my implementation in each .h:
AppDelegate *delegate;
在 init
函数中,将单例实例放到该变量中吗?
And inside the init
function, put the singleton instance to that variable?
对不起,如果这是不合时宜的,但我认为这是一个逻辑问题,人们遇到并解决问题.
Sorry if this was off structure, but I think it's a logical question people have a problem encountering and solving.
推荐答案
也许您需要重新考虑如何使用App Delegate?在我看来,也许您的课堂设计不是很好.
Maybe you need to reconsider how you are using the App Delegate? It sounds to me like perhaps you are not making a very good class design.
无论如何,这是一种简化操作的方法.不要将它放在init中,仅在需要时使用它.
Regardless, here's a way to make it easy. Don't put this in init just use it when you need it.
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
自然地,将MyAppDelegate替换为您的应用程序委托的实际类名.
Naturally, replace MyAppDelegate with the actual class name of your app delegate.
这篇关于最佳应用代表实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!