导入AppDelegate [英] importing AppDelegate

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

问题描述

通常我在我的AppDelegate中初始化我的模型类变量,所以他们可以被不同的ViewControllers使用,而不传递他们的实例跨类文件。但是,每次我将AppDelegate导入.m文件以访问这些变量的数据时,我觉得我做错了。



这是正确的访问方式这些变量或者我应该做不同的吗?



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



id appDelegate = [[UIApplication sharedApplication] delegate];



从概念上讲,我想知道这是否是接受的与应用程序模型类交互的方式。看来,对我来说,应用程序的AppDelegate管理整个应用程序。因此,将这个类导入到应用程序类链中其他类中似乎有违直觉。

解决方案


这是访问这些变量的正确方法,或者我应该
做不同的吗?


不同的人对此有不同的看法。我喜欢的样式是让应用程序委托将必要的信息传递给第一个视图控制器,并且让视图控制器将它传递给它创建的任何视图控制器,等等。主要原因是它阻止子视图控制器依赖于他们没有业务知道的事情。



如果你有一些细节编辑器,例如,你想要能够通过那个编辑器,它需要做什么工作。如果你给它那个信息,编辑器是完全灵活的 - 它会编辑你给它的任何信息。如果编辑器知道它应该从某个外部对象(如应用程序委托)获取数据,那么它会失去一定程度的灵活性 - 它只能从它所知道的事物中获取数据。 / p>

因此,在应用程序委托中设置数据模型是很好的。但是,当提到访问模型时,请考虑:告诉,不要问。也就是说,让应用程序委托告诉第一个视图控制器使用什么模型对象,并让该控制器告诉下一个,等等。如果你必须 ,你必须知道要求,这就是依赖关系开始走向错误的方向。


每次我将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?

EDIT: 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];

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.

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天全站免登陆