iPhone 正确使用应用程序委托 [英] iPhone proper usage of Application Delegate

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

问题描述

我希望能够通过我的应用程序中的任何地方引用某些状态/对象.例如,用户登录到他们的应用程序,我需要调用 Web 服务并检索用户信息.然后我希望能够从应用程序的任何地方访问这些信息,如下所示:

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:

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.

推荐答案

通常,您应该只在以下情况下将事物连接到应用程序委托:

Normally, you should only connect things to the app delegate if they:

  • 从与应用程序委托相同的 NIB 文件创建(即单窗口界面中的静态 UI 元素)
  • 与通过应用程序委托的应用程序级事件处理相关联(如首选项窗口的菜单项)

对于其他一切,您应该创建一个单例来管理对它们的访问.

For everything else, you should create a singleton which manages access to them.

Jason Coco 建议通过应用程序控制器进行路由.在我的程序中,我通常会避免这种情况,因为我认为它在顶层承担了太多责任——我认为事情应该在可能的情况下进行自我管理,并且只有在对等之间需要协调时才应该使用更高级别的管理——级别模块.

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.

我不会链接我自己的博客,但如果你用谷歌搜索我和单身人士,你可能会找到我写的一篇更详细的文章.

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.

这篇关于iPhone 正确使用应用程序委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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