将大多数逻辑和模型放在appDelegate中是否可以? [英] Is it ok to place most logic and models in the appDelegate?

查看:84
本文介绍了将大多数逻辑和模型放在appDelegate中是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的大多数应用程序,我已将所有逻辑放在类中,每个ViewController也会获得类的引用,或者创建/释放对象本身。

For most of my apps, I have placed all the logic in classes, that each ViewController would get a reference too the class, or create/release the object itself.

我刚开始阅读有关IOS的书,作者似乎想把app逻辑放在appDelegate中,而viewcontrollers只是将动作转发到appDelegate方法谁做了真正的工作。

I just started reading a book on IOS, and the author seems to like to put the app logic in the appDelegate, and the viewcontrollers just relay the actions to the appDelegate methods who do the real work.

作者是这样做的,因为它们是简单的例子,或者这是我应该学习的东西,并开始在我的应用程序中做什么? / p>

Is the author just doing this because they are simple examples, or is this something I should learn, and start to do in my apps?

推荐答案

首先,请参阅什么最能描述应用程序代表?它如何适用于整个概念?

应用程序委托是应用程序的委托。它不是容纳你不知道的地方的地方。它不是全局变量的存储位置。它是 UIApplication 对象的委托。所以这是放置与启动应用程序,终止,切换到后台等相关的代码的正确位置。等与应用程序如何适应操作系统有关的事情。

The application delegate is the delegate for the application. It is not the place to hold everything you don't know where else to put. It is not the storage place for globals. It is the delegate for the UIApplication object. So it is the correct place to put code related to starting the application, terminating, switching to and from the background, etc. Things that have to do with how the application fits into the OS.

app delegate是一个控制器,所以它不应该保存数据。数据在模型中。应用程序委托可以在启动时创建模型并将其交给其他控制器,但它不是模型的API。通常,模型是单例,而不是由应用程序委托创建。这两种方法都有优势。

The app delegate is a controller, so it should not hold data. Data goes in the model. The app delegate may create the model at startup and hand it to other controllers, but it isn't the API to the model. Often the model is a singleton instead of being created by the app delegate. Both approaches have advantages.

大多数示例代码将模型代码放在app委托中,因为对于简单的示例,它需要的代码少一些。但在实际程序中,它使应用程序委托过于复杂,并且显着损害了代码重用。您的应用程序委托通常应该非常小,并且其中的大多数方法应该是< UIApplicationDelegate> 的一部分。

Most example code puts the model code in the app delegate because for simple examples it requires a little less code. But in real programs it makes the app delegate far too complicated, and significantly hurts code reuse. Your app delegate should generally be pretty small, and most of the methods in it should be part of <UIApplicationDelegate>.

这篇关于将大多数逻辑和模型放在appDelegate中是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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