简体   繁体   English

iPhone正确使用Application Delegate

[英]iPhone proper usage of Application Delegate

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. 例如,用户登录到他们的应用程序,我需要调用Web服务并检索用户信息。 Then I want to be able to access this information from anywhere in the application with something like the following: 然后,我希望能够从应用程序的任何位置访问此信息,如下所示:

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? 是否在应用程序委托中将实例变量设置为User对象,并在需要时从那里引用它是一种糟糕的方式来实现它? I typically set it there upon the user's login. 我通常根据用户的登录设置它。

Wanted to hear how the pros handle this one. 想听听专业人士如何处理这个。

Normally, you should only connect things to the app delegate if they: 通常,您应该只将以下内容连接到应用代理:

  • Were created from the same NIB file as the app delegate (ie static UI elements in single window interfaces) 从与应用委托相同的NIB文件创建(即单窗口界面中的静态UI元素)
  • Are associated with application-level event handling that passes through the app delegate (like the menu item for the Preferences Window) 与通过应用程序委托的应用程序级事件处理相关联(如“首选项”窗口的菜单项)

For everything else, you should create a singleton which manages access to them. 对于其他所有内容,您应该创建一个管理对它们的访问权限的单例。

Jason Coco suggested routing through the Application Controller. Jason Coco建议通过应用程序控制器进行路由。 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. 在我的程序中,我通常会避免这种情况,因为我认为它在顶层承担了太多责任 - 我认为事情应尽可能自我管理,而且只有在同行之间需要协调时才应使用更高级别的管理。级别模块。

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. 我不会链接我自己的博客,但如果你谷歌我和单身,你可能会发现我写的帖子更详细。

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

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 MyCommon singleton is then used anywhere in my application as follows: 然后在我的应用程序中的任何地方使用MyCommon单例,如下所示:

int user = [MyCommon singleton].user;

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). 您的应用程序的控制器可能与应用程序委托相同或不同(在大多数简单应用程序中,它是相同的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM