views:

417

answers:

3

So I've read most of the Jeff LaMarche book, and I still can't seem to understand this...

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)

+3  A: 

Use MVC pattern (Model View Controller).

Your model stores that state (e.g. 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 (e.g. you link button press with a method on controller). Controller updates your model (increment/decrement variables, perform actions, change user name and so on).

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.

stefanB
I think I understand, but the actual implementation of MVC is hard to fathom. For instance, I might have an instance of a Settings object in my MainViewController, but how do I access that in my FlipSideViewController?
yukon
Controllers need a way to access the model. This can be achieved in various ways. In your example, the Settings object (the model) could use the [singleton pattern][1], so that every controller could access it using [Settings sharedSettings]. [1]: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
Nikolai Ruhe
If your app just stores a bunch of simple values, you can use the defaults system instead of implementing your own model.http://developer.apple.com/documentation/Cocoa/Conceptual/UserDefaults/UserDefaults.html
Nikolai Ruhe
+3  A: 

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

The simplest is probably to use the AppDelegate as a stand in for your model and controller.

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

Roger Nolan
A: 

There is a great discussion about MVC on this site.

In particular look at the answer by Clint - and his description of adding a parameter to the init method of your controller (e.g. initWarehouse: ) - as this is how you can pass a model between your views.

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.

TimM