简体   繁体   English

视图之间的持久数据

[英]Persistent data between views

So I've read most of the Jeff LaMarche book, and I still can't seem to understand this... 因此,我已经阅读了杰夫·拉马什(Jeff LaMarche)的大部分书籍,但我似乎仍然无法理解...

How do I persist data between various views? 如何在各种视图之间保留数据? For instance, with a standard 'Utility Application' template, I want to be able to share variables between the frontside and the flipside... How to do? 例如,使用标准的“实用程序”模板,我希望能够在正面和反面之间共享变量。该怎么办?

(I also want to write those variables to disk so the app doesn't lose them when it quits, but i guess that's another story) (我也想将这些变量写入磁盘,以便应用程序退出时不会丢失它们,但我想那是另一回事了)

Use MVC pattern (Model View Controller). 使用MVC模式(模型视图控制器)。

Your model stores that state (eg variables) and logic to be applied to them. 您的模型存储该状态(例如变量)和要应用于它们的逻辑。 Your view displays values stored in model. 您的视图显示存储在模型中的值。 Your controller has links to both model and view. 您的控制器具有指向模型和视图的链接。

View displays your model and user can interact with it. 视图显示您的模型,用户可以与它进行交互。 View does not apply changes to model , view only reads model to display values. 视图不对模型应用更改,视图仅读取模型以显示值。 View sends notifications to controller about events (eg you link button press with a method on controller). View向控制器发送有关事件的通知(例如,您将按下按钮与控制器上的方法链接在一起)。 Controller updates your model (increment/decrement variables, perform actions, change user name and so on). Controller将更新您的模型(增加/减少变量,执行操作,更改用户名等)。

After controller updated model in notify view to redisplay itself. 控制器更新模型后,在通知视图中重新显示自身。 As view has link to model it reads new values and displays them. 由于视图具有到模型的链接,因此它将读取并显示新值。

You can have multiple views connected to the same controller or multiple controllers manipulating your model. 您可以将多个视图连接到同一控制器,也可以将多个视图用于操纵模型。

The same goes for saving/restoring data. 保存/恢复数据也是如此。 Your controller can load data from file or just pass file to your model to initialize itself. 您的控制器可以从文件加载数据,也可以仅将文件传递给模型以对其进行初始化。 Once model is initialized controller request view to update itself. 一旦模型初始化,控制器请求视图即可更新自身。

And so on. 等等。

Have a read on Model-View-Controller pattern in apple documentation, it comes with examples. 阅读Apple文档中的Model-View-Controller模式,它附带示例。

I think you're maybe missing the point that none of this happens by magic or 'for free'. 我认为您可能忘记了魔术或“免费”都不会发生的情况。 The bottom line is that you need to write code to set up the relationships and transfer data between the views. 最重要的是,您需要编写代码来建立关系并在视图之间传输数据。

There are many ways to do this. 有很多方法可以做到这一点。 As Stefan says, the best way is to use the MVC design pattern. 正如Stefan所说,最好的方法是使用MVC设计模式。 There are simpler ways though. 有更简单的方法。 Nikolia points out you can store your values to the defaults system and access that from each of your views. Nikolia指出,您可以将值存储到默认系统,并从每个视图访问该值。

The simplest is probably to use the AppDelegate as a stand in for your model and controller. 最简单的方法可能是将AppDelegate用作模型和控制器的替代品。

Add the data to your AppDelegate's definition and then set it from your view control methods and read it from the view set up code. 将数据添加到AppDelegate的定义中,然后从视图控制方法进行设置,并从视图设置代码中读取数据。 It's not pretty or clever but it'll do the job quickly and easily for your siple app and let you get on with learning other things. 它虽然不漂亮,也不聪明,但是它将为您简单的应用程序快速轻松地完成工作,并让您继续学习其他东西。

There is a great discussion about MVC on this site. 在此站点上,有关MVC的讨论非常多。

In particular look at the answer by Clint - and his description of adding a parameter to the init method of your controller (eg initWarehouse: ) - as this is how you can pass a model between your views. 尤其要看一下Clint的答案-以及他向控制器的init方法添加参数的描述(例如initWarehouse:)-因为这是您可以在视图之间传递模型的方式。

As for saving - that model that you pass around can be written out using one of the mechanisms described in the book your are reading. 至于保存-您可以使用正在阅读的书中介绍的机制之一写出您所经过的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM