在哪里存储主表视图数据? (appDelegate或rootViewController) [英] where to store main table view data? (appDelegate or rootViewController)

查看:137
本文介绍了在哪里存储主表视图数据? (appDelegate或rootViewController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关如何存储iPhone应用程序主列表数据的建议,如下所示?

Any advice on where to store the main list data for an iPhone application like the following?


  • NavigationController 基于

  • 1级(主屏幕)是项目列表。因此它使用表格视图(项目表)

  • 级别2_EDIT:是一个可以通过单击编辑从主屏幕进入的视图。在这里,您可以添加要添加到主视图列表中的文本。

  • 级别2_DETAIL:是一个可以通过单击单元格从主屏幕进入的视图。

  • NavigationController based
  • Level 1 (main screen) is a List of Items. Hence it uses a table view (table of items)
  • Level 2_ Is a view you can get to from the main screen by clicking EDIT. Here you can add text to be added to the main view list
  • Level 2_DETAIL: Is a view you can get to from the main screen by clicking on a cell.

现在假设实现是(粗略概述):
* MainView - appDelegate(持有 UIWindow & UINavigationController
* RootViewController - 主要项目列表的表格视图(?变量在这里?)
* EditViewController - 输入要添加到主列表的文本
* DetailViewController - 显示记录的详细信息

Now assuming the implementation is (rough overview): * MainView - appDelegate (holds UIWindow & UINavigationController) * RootViewController - table view of the main items list (? variables here ?) * EditViewController - input text to add to main list * DetailViewController - shows detail of record

问题 - 在哪里举行 NSArray 保留主要的物品清单?它应该在 RootViewController 中,表视图存在哪个显示它?或者它应该在 ApplicationDelegate 中更高?我注意到当你从 RootViewController 转到 EditViewController 时,那么在这个编辑视图中你必须将项目添加到数组,所以 EditViewController 中的代码更容易从 AppDelegate 访问主数组(相反)到 RootViewController )?

Question - Where to hold the NSArray that keeps the main list of items? Should it be in the RootViewController where the Table View exists that displays it? Or should it be higher up in the ApplicationDelegate? I note that when you go from RootViewController to EditViewController, then in this edit view you would have to ADD items to the array, so would it be easier for the code in the EditViewController to access the main array from the AppDelegate (as opposed to the RootViewController)?

(注意 - 仍然没有制作具有特定模型对象的应用,重新MVC,但是,不确定这是否应该出现在图片中。)

(Note - still haven't made an app that has a specific model object, re MVC, yet, so not sure if this should come into the picture.)

推荐答案

您的数据成为表视图的数据源虽然它不必,但包含表视图的视图控制器通常会执行数据源的角色。视图控制器也可以是EditViewController的委托,因此EditViewController向它发送消息,以便它可以更新数组。

Your data becomes the table view's data source, and although it doesn't have to, the view controller that contains the table view often does the data source's role. The view controller can also be a delegate for the EditViewController, so the EditViewController sends a message to it so it can update the array.

Apple的CoreDataBooks示例项目显示类似的体系结构。你可能想看看。

Apple's CoreDataBooks sample project show similar architecture. You may want to take a look.

将数组放在应用程序委托中通常不是一个好主意。虽然它可以给你一点方便,但现在你的课程完全依赖你的申请代表。

Having the array in the application delegate is often not a great idea. Although it can give you a little bit of convenience, now your classes totally depend on your application delegate unnecessarily.

你的桌子视图显示您的数据。这对应于MVC设计模式中的查看。我假设 RootViewController 是表视图的视图控制器,它在模式中充当控制器。您尚未确定其位置的数据对应于模型 RootViewController 的作用是连接模型查看

Your table view shows your data. This corresponds to View in the MVC design pattern. I assume RootViewController is the view controller of the table view, which acts as a Controller in the pattern. Your data, the location of which is not decided yet, corresponds to Model. The role of RootViewController becomes connecting the Model and View.

MVC模式的理想或原因是隔离模型和视图,因此模型可以与具有适当控制器的其他视图一起使用,并且视图也可以与具有适当控制器的其他模型一起使用。例如,您的 RootViewController 将为表视图提供数据。它将以表格视图的语言指定数据,例如部分和行的数量,单元格的内容等。如果要以不同的方式显示数据,例如图形,控制器将访问相同的数据(模型)并提供具有不同表示的图形视图相同的数据。模型不需要改变,视图也不需要改变。您只为模型和视图的每个组合编写控制器。

The ideal, or the reason of the MVC pattern is to isolate the model and the view, so the model can work with other views with appropriate controllers, and the view can also work with other models with appropriate controllers. For example, your RootViewController will provide the table view with data. It will specify the data in the language of table view, e.g. the number of sections and rows, the contents of cells, etc. If you want to present the data in a different way, for example a graph, your controller will access the same data (model) and provide the graph view with a different representation of the same data. The model need not change, neither do the views. You write only the controller, for each combination of a model and a view.

因此,理想情况下,您将为模型设置不同的类。在这个类中,您将存储数组,并为控制器提供通用接口,可以与数据进行交互。

Ideally, therefore, you will have a different class for the model. In this class, you will store the array, and provide a general interface for controllers can interact with the data.

但是,它通常不是必需的,因为它不太可能您将经常再次使用模型类,或者因为模型本身太简单所以它可以在任何地方轻松实现。例如,如果表的数据是一个简单数组,则NSArray对象通常足以用于模型的角色。因此,这里有一个想法将控制器和模型组合成一个对象

However, it's often not that necessary, either because it's unlikely that you will use the model class often again, or because the model itself is way too simple so it can be easily implemented anywhere. For example, if your data for the table is a simple array, an NSArray object is often sufficient for the model's role. Therefor, here comes an idea that to combine the controller and the model into a single object.

这就是为什么你的表有意义视图控制器通常充当表视图的数据源。

This is why it makes sense that your table view controller often acts as the data source of the table view.

但是,将数据存储在应用程序委托中是一个完全不同的想法。现在,应用程序委托成为您的模型,但这没有意义,因为应用程序委托仅用于特定的应用程序。为什么你会有一个完全依赖于单个应用程序的单独模型对象?此外,如果您的表视图直接与应用程序委托交互,则意味着现在您的视图无法用于其他应用程序,因为它现在取决于特定应用程序的应用程序委托。

However, storing the data in the application delegate is a completely different idea. Now the application delegate becomes your model, but which does not make sense, because the application delegate is only used for the specific application. Why would you have a separate model object that totally depends on a single application? Also, if your table view directly interacts with the application delegate, it means that now your view cannot work for other applications either because it now depends on the specific application's application delegate.

通常,人们想要在应用程序委托中拥有数据的原因是,应用程序中的任何对象都可以使用 [UIApplication sharedApplication] .delegate 。 M-V-C关系并不总是很简单。例如,您的EditViewController还需要访问相同的模型。为此,您必须编写一些代码以使表格视图和编辑视图都可以访问模型。如果您在应用程序委托中拥有数据,则无需执行任何操作,因为您可以通过访问应用程序委托来神奇地访问数组。

Often the reason why people are tempted to have data in the application delegate is, that the application delegate is easily accessible by any objects in the application by using [UIApplication sharedApplication].delegate. The M-V-C relationship is not always very simple. For example, your EditViewController also need to access the same model. To do this, you have to write some code to make the model accessible by both the table view and the edit view. If you have the data in the application delegate, you don't need to do anything, because you can magically access the array by accessing the application delegate.

但这都是。以编程时间节省几分钟,以破坏软件架构为代价。我不是原教旨主义者,有时我会使用应用程序委托存储一些数据,因为我绝对不确定提供格式良好的接口是不值得的,但这种情况很少见。

But that's all. A few minute's saving in your coding time, for the price of ruining your software architecture. I'm not a fundamentalist, and I do sometimes use application delegate to store some data when I'm absolutely sure that it's not worth to provide well-formed interfaces, but it's rare.

那么如何将编辑视图连接到合并到表视图控制器的数据?可能有多种方式。我之前建议的是让编辑视图控制器对表视图控制器( delegate )具有弱引用并发送定义的消息,例如 - (void)editViewController:(EditViewController * editViewController)didFinishEditing:(id)someData 。通过这种方式,您可以将此编辑视图控制器与其他视图控制器一起使用,只要它们使用相同的协议即可。但其他人可能会为它实现不同的接口。

So how should you connect your edit view to the data merged into the table view controller? There could be multiple ways. What I suggested before is let the edit view controller has a weak reference to the table view controller (delegate) and send a defined message, for example - (void)editViewController:(EditViewController *editViewController) didFinishEditing:(id) someData. In this way, you can use this edit view controller with some other view controllers as long as they use the same protocol. But others may implement different interfaces for it.

这篇关于在哪里存储主表视图数据? (appDelegate或rootViewController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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