在没有共享根视图控制器的情况下在多个视图之间保留数据源

发布于 2024-10-05 16:11:42 字数 738 浏览 5 评论 0原文

我目前正在开发一个在 UITabBars 内使用 UINavigationController 的应用程序。选项卡栏对应于 UITableViews 以及地图视图。但是,应用程序的根视图控制器不是 UITableView 自定义控制器和地图视图控制器的父级或直接父级。我还有一个创建 NSDictionary 对象的 p-list;它是我用来填充表和地图中的条目的数据源。

那么,如果没有根视图控制器,我应该如何从数据源创建 NSDictionary 对象?在我看来,最简单的方法是简单地在每个视图控制器中为需要数据的视图重新创建对象。因为我没有那么多视图,而且 p 列表也没有那么长,所以内存不应该是问题。然而,我确实知道这一切都是非常低效的。

是否有任何替代实现,以便我不必在每个视图控制器中重新创建 NSDictionary ?


本教程以单例指南为特色,简洁地解释了一切: http://www.cocoanetics.com/2009/05/ the-death-of-global-variables/

我现在唯一担心的是,如果多个视图控制器每个都调用单例,它们是否都会生成 NSDictionary 的多个实例,并且无法这不是可以想象仍然占用大量内存吗?

I'm currently working on an app that uses a UINavigationController inside UITabBars. The tab bars correspond to both UITableViews as well as a Map View. However, the root view controller of the app is not the parent, or direct parent, of the UITableView custom controllers and map view controller. I also have a p-list that creates NSDictionary objects; it is the datasource that I am using to populate entries in the tables and the map.

So, without a root view controller, how should I create the NSDictionary objects from the data source? It seems to me that the easiest way is to simply recreate the object in each view controller for a view that needs the data. Because I don't have that many views and the p-list isn't that long, memory shouldn't be an issue. However, I do know that this is all very inefficient.

Is there any alternative implementation so that I don't have to recreate the NSDictionary in each view controller?


This tutorial featuring the singleton guide neatly explains everything:
http://www.cocoanetics.com/2009/05/the-death-of-global-variables/

My only worry now is if multiple view controllers each call the singleton, wouldn't they all be generating multiple instances of the NSDictionary, and couldn't that conceivably still take up a lot of memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2024-10-12 16:11:42

您需要一个数据模型对象来存储应用程序的数据。

数据模型是可从应用程序中的任何位置访问的定制的独立对象。数据模型对象对任何视图或视图控制器一无所知。它只存储数据以及数据之间的逻辑关系。

当应用程序的不同部分需要写入或读取数据时,它们会正确读取数据模型。在您的情况下,view1 会在卸载时将其数据保存到数据模型中,然后 view2 会在加载时从数据模型中读取该数据(反之亦然)。

在正确设计的应用程序中,任何两个视图控制器都不应有权访问另一个控制器的内部数据。 (视图控制器需要知道另一个控制器存在的唯一原因是它是否必须触发另一个控制器的加载。)

创建数据模型的快速而肮脏的方法是将属性添加到应用程序委托,然后使用以下方法从视图控制器调用应用程序委托:

MyAppDelegateClass *appDelegate=[[UIApplication sharedApplicaton] delegate];
myLocalProperty=appDelegate.someDataModelProperty;

这适用于小型项目,但随着数据变得复杂,您应该为数据模型创建一个专用类。

将数据放置在 init 方法或 viewDidLoad 中将不起作用,因为在选项卡栏中,用户可以来回切换,而无需卸载视图或重新初始化视图控制器。

检索更改数据的最佳位置是在 viewWillAppear 控制器方法中。这样,每次用户切换到该选项卡时,数据都会更新。

You need a data model object that stores the data for application.

A data model is a customized, standalone object accessible from anywhere in the application. The data model object knows nothing about any views or view controllers. It just stores data and the logical relationships between that data.

When different parts of the app need to write or read data, they right and read to the data model. In your case, view1 would save its data to the data model when it unloads and then view2 would read that data from the data model when it loads (or vice versa.)

In a properly designed app, no two view controllers should have access to the internal data of another controller. (The only reason a view controllers needs to know of the existence of another controller is if it has to trigger the loading of that other controller.)

The quick and dirty way to create a data model is to add attributes to the app delegate and then call the app delegate from the view controllers using:

MyAppDelegateClass *appDelegate=[[UIApplication sharedApplicaton] delegate];
myLocalProperty=appDelegate.someDataModelProperty;

This will work for small project but as your data grows complex, you should create a dedicated class for your data model.

Placing the data in an init method or a viewDidLoad won't work because in a tabbar the users can switch back and forth without unloading the view or reinitializing the view controller.

The best place to retrieve changing data is in the viewWillAppear controller method. That way the data will be updated every time the user switches to that tab.

荭秂 2024-10-12 16:11:42

实际上,数据模型通常放置在某种单例中(您自己的单例,或者是一种单例的应用程序委托)。

In practical terms, a data model is usually either placed in some kind of a singleton (either your own, or the app delegate which is a kind of singleton).

那些过往 2024-10-12 16:11:42

您应该阅读模型-视图-控制器 (MVC) 架构。具体来说,您可能需要引入一个数据模型,在初始化期间创建并填充字典,然后从所有视图控制器访问它。

You should read up on Model-View-Controller (MVC) architecture. Specifically, you'll probably want to introduce a data model where you would create and populate the dictionary during initialization and then access it from all of your view controllers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文