在没有共享根视图控制器的情况下在多个视图之间保留数据源 [英] Persisting a datasource between multiple views without shared root view controller

查看:84
本文介绍了在没有共享根视图控制器的情况下在多个视图之间保留数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发在UITabBars内使用UINavigationController的应用程序.标签栏既对应于UITableViews,也对应于地图视图".但是,应用程序的根视图控制器不是UITableView自定义控制器和地图视图控制器的父级或直接父级.我还有一个创建NSDictionary对象的p列表.这是我用来填充表格和地图中条目的数据源.

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.

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

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.

是否有其他替代实现,所以不必在每个视图控制器中重新创建NSDictionary?

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

以单例指南为特色的本教程巧妙地解释了所有内容: http://www.cocoanetics.com/2009/05/死亡全局变量/

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

我现在唯一担心的是,如果多个视图控制器各自调用单例,它们是否都将生成NSDictionary的多个实例,并且难道这还无法占用大量内存吗?

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?

推荐答案

在实践中,数据模型通常要么放置在某种单例中(要么是您自己的,要么是某种单例的应用程序委托)

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).

这篇关于在没有共享根视图控制器的情况下在多个视图之间保留数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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