导入 AppDelegate [英] importing AppDelegate

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

问题描述

我经常在 AppDelegate 中初始化模型类变量,以便不同的 ViewController 可以使用它们,而无需跨类文件传递它们的实例.但是,每次我将 AppDelegate 导入 .m 文件以访问这些变量的数据时,我都觉得自己做错了.

Often times I initialize my model class variables in my AppDelegate so they can be used by different ViewControllers without passing their instance across class files. However, every time I import AppDelegate into a .m file to access these variable's data I feel like I'm doing some wrong.

这是访问这些变量的正确方法还是我应该做一些不同的事情?

Is this the the correct way for accessing these variables or should I be doing something differently?

我的问题不是如何访问变量.我目前使用这行代码来获取我的 appDelegate 实例:

My problem isn't how to access the variables. I currently use this line of code to get my appDelegate instance:

id appDelegate = [[UIApplication sharedApplication] delegate];

从概念上讲,我想知道这是否是与应用程序的模型类交互的可接受方式.在我看来,应用程序的 AppDelegate 似乎对整个应用程序进行管理.因此,将此类导入到应用程序类链下游的其他类中似乎有悖常理.

Conceptually, I want to know if this is the accepted way to interact with an application's model classes. It seems, to me, that an application's AppDelegate manages the application overall. So it seems counterintuitive to import this class into other classes further down an application's class chain.

推荐答案

这是访问这些变量的正确方法还是我应该做一些不同的事情?

Is this the the correct way for accessing these variables or should I be doing something differently?

你会发现不同的人对此有不同的看法.我更喜欢的风格是让应用程序委托将必要的信息传递给第一个视图控制器,然后让该视图控制器将其传递给它创建的任何视图控制器,依此类推.这样做的主要原因是它可以防止子视图控制器依赖于他们不知道的事情.

You'll find that different people have different opinions about this. The style that I prefer is to have the app delegate pass the necessary information to the first view controller, and have that view controller pass it on to whatever view controllers it creates, and so on. The main reason for this is that it prevents child view controllers from depending on things that they have no business knowing about.

例如,如果您有一些细节编辑器,您希望能够准确地传递该编辑器完成其工作所需的内容.如果你给它那个信息,编辑器是完全灵活的——它会编辑你给它的任何信息.如果编辑器知道它应该从某个外部对象(例如应用程序委托)获取数据,那么它就会失去一定程度的灵活性——它只能从它知道的事物中获取数据.

If you have some detail editor, for example, you want to be able to pass that editor exactly what it needs to do its work. If you give it that information, the editor is completely flexible -- it'll edit any information that you give it. If the editor knows that it should get its data from some external object, like the app delegate, then it loses some degree of flexibility -- it can only get data from the thing that it knows about.

因此,可以在应用程序委托中设置您的数据模型.但是在提供对模型的访问时,请考虑:告诉,不要问.也就是说,让应用委托告诉第一个视图控制器要使用什么模型对象,然后让该控制器告诉下一个,依此类推.如果您必须询问,您必须知道问谁,这就是依赖关系开始朝错误方向发展的地方.

So, it's fine to set up your data model in the app delegate. But when it comes to providing access to the model, think: tell, don't ask. That is, have the app delegate tell the first view controller what model object to use, and have that controller tell the next one, and so on. If you have to ask, you have to know who to ask, and that's where the dependencies start heading in the wrong direction.

每次我将 AppDelegate 导入 .m 文件以访问这些变量的数据我觉得我做错了.

every time I import AppDelegate into a .m file to access these variable's data I feel like I'm doing some wrong.

相信这种直觉.想想为什么感觉不对.

Trust that instinct. Think about why it feels wrong.

这篇关于导入 AppDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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