应用程序设计和应用程序 [英] Application Design and AppDelegate

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

问题描述

我正在开发一个iPhone应用程序,用于一些我一直在研究的甜蜜的本科研究。遗憾的是,我的学校不提供软件工程/设计课程,所以当谈到OO设计的最佳做法的问题,我做很多阅读。

I am developing an iPhone app for some sweet undergrad research I've been working on. Sadly, my school doesn't offer software engineering / design classes so when it comes to questions of best practices in OO Design, I do a lot of reading.

我的困境:

我的应用程序加载一个视图(v1),在用户按钮单击时,v1的控制器类执行一个动作方法。这个动作方法应该用对象填充数组。之后,用户将再次执行操作或单击另一个选项卡以加载另一个视图。应用程序中的其他视图将使用v1填充的数组。

My application loads a view (v1) where, upon user's button click, v1's controller class executes an action method. This action method should fill an array with objects. After that, the user will either execute the action again or click a different tab to load another view. Other views in the application will use the array that v1 populated.

那么,应该在哪里声明这个共享数组?现在,它在AppDelegate类中,当我测试没有GUI的功能。我应该抓住AppDelegate单例并添加项目到它在v1ViewController吗?是否应声明为静态?

So, where should this shared array be declared? Right now, it's in the AppDelegate class from when I was testing features without a GUI. Should I grab the AppDelegate singleton and add items to it in the v1ViewController? Should it be declared as static?

感谢您的帮助!

^水牛城

编辑:

跟进问题:当与单例进行交互时,这是更好的交谈方式:

Follow-up Question: When interacting with a singleton, which is the better way to talk to it:

[[MyAwesomeSingleton sharedInstance] gimmeSomePizza]; 

MySingleton *s = [MySingleton sharedInstance]; 
[s gimmeSomePizza]; 

我想我想知道的是,你每次都调用sharedInstance方法定义指向sharedInstance的指针,然后引用指针?

I guess what I'm wondering is, do you make the sharedInstance method call every time or do you define a pointer to the sharedInstance and then reference the pointer?

推荐答案

使用应用程序委托存储视图和视图之间共享的数据控制器是合理和适当的。

Using the app delegate to store data that's shared between views and view controllers is reasonable and appropriate.

在我的应用程序中,我查看应用程序委托作为MVC的控制器部分,UIViews和视图控制器都是视图 。我更喜欢使用一种称为被动视图的MVC变体,保留模型和视图部分我的应用程序严格隔离,只有控制器连接它们。

In my apps, I view the app delegate as the controller part of MVC, with UIViews and view controllers all being part of the "view". I prefer to use a variant of MVC called Passive View that keeps the model and view parts of my app strictly segregated with only the controller connecting them.

我假设你存储的对象数组是你的应用程序的模型,所以将它们存储在你的app delegate是有意义的。正如Daniel D所说,没有必要使它静态。

I'm assuming that the array of objects you're storing is your app's model, so storing them on your app delegate makes sense. As Daniel D said, there's no need to make it static.

应用程序委托是真正的程序的核心。在 -applicationDidFinishLaunching:方法中创建并初始化模型和视图,并在 -applicationWillTerminate:。当您的视图控制器接收到更改模型的事件时,您可以调用应用程序代理的方法来进行更改。

The app delegate is really the heart of your program. You create and initialize your model and views in your -applicationDidFinishLaunching: method and save your model data and view state in -applicationWillTerminate:. When your view controllers receive events that changes your model, you can call methods on your app delegate to make those changes.

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

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