objective-c iPhone 正确使用应用程序委托
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/338734/
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
iPhone proper usage of Application Delegate
提问by Coocoo4Cocoa
I'm looking to be able to reference certain state/objects through anywhere in my application. For instance, a user logs in to their application, I need to call a web service and retrieve the users information. Then I want to be able to access this information from anywhere in the application with something like the following:
我希望能够通过我的应用程序中的任何地方引用某些状态/对象。例如,用户登录到他们的应用程序,我需要调用 Web 服务并检索用户信息。然后我希望能够从应用程序的任何地方访问这些信息,如下所示:
myAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
user = delegate.u;
Is setting an instance variable as a User object in the app delegate and referencing it from there when needed a poor way of going about it? I typically set it there upon the user's login.
在应用程序委托中将实例变量设置为用户对象并在需要时从那里引用它是一种糟糕的处理方式吗?我通常在用户登录时将其设置在那里。
Wanted to hear how the pros handle this one.
想听听专业人士如何处理这个问题。
采纳答案by Matt Gallagher
Normally, you should only connect things to the app delegate if they:
通常,您应该只在以下情况下将事物连接到应用程序委托:
- Were created from the same NIB file as the app delegate (i.e. static UI elements in single window interfaces)
- Are associated with application-level event handling that passes through the app delegate (like the menu item for the Preferences Window)
- 从与应用程序委托相同的 NIB 文件创建(即单窗口界面中的静态 UI 元素)
- 与通过应用程序委托的应用程序级事件处理相关联(如首选项窗口的菜单项)
For everything else, you should create a singleton which manages access to them.
对于其他一切,您应该创建一个单例来管理对它们的访问。
Jason Coco suggested routing through the Application Controller. In my programs I normally avoid this, as I think it puts too much responsibility at the top level -- I think things should self-manage where possible and that higher level management should only be used when there is a requirement for coordination between peer-level modules.
Jason Coco 建议通过应用程序控制器进行路由。在我的程序中,我通常会避免这种情况,因为我认为它在顶层承担了太多责任——我认为事情应该在可能的情况下进行自我管理,并且只有在对等之间需要协调时才应该使用更高级别的管理——级模块。
I'm not going link my own blog but if you Google me and singletons you'll probably find a post I wrote going into more detail.
我不会链接我自己的博客,但如果你用谷歌搜索我和单身人士,你可能会找到我写的一篇更详细的文章。
回答by Brad The App Guy
Matt is a bit too modest. His posting on the subject is one of the best I have read, and deserves a link. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
马特有点太谦虚了。他在这个主题上的帖子是我读过的最好的帖子之一,值得一个链接。 http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
回答by e.James
I don't see any problem with your approach. I usually use a singleton to handle this situation:
我认为你的方法没有任何问题。我通常使用单例来处理这种情况:
// MyCommon.h:
@interface MyCommon
class MyCommon : NSObject
{
int user;
};
@property(assign) int user;
+ (MyCommon *)singleton;
@end
// MyCommon.m:
@implementation MyCommon
static MyCommon * MyCommon_Singleton = nil;
+ (MyCommon *)singleton
{
if (nil == MyCommon_Singleton)
{
MyCommon_Singleton = [[MyCommon_Singleton alloc] init];
}
return MyCommon_Singleton;
}
@end
The MyCommonsingleton is then used anywhere in my application as follows:
该MyCommon单然后在我的应用程序任何地方使用,如下所示:
int user = [MyCommon singleton].user;
回答by Jason Coco
Usually you would ask your application's controller for this information and it would be responsible for knowing how to store it/look it up in whatever data model exists. Your application's controller may or may not be the same as the applications delegate (in most simple applications, it is the same).
通常,您会向应用程序的控制器询问此信息,它负责知道如何在存在的任何数据模型中存储/查找它。您的应用程序的控制器可能与应用程序委托相同,也可能不同(在大多数简单的应用程序中,它是相同的)。

