I have some data strings that from one screen of my app that I would like to store (in a model) and then retrieve if the user navigates back to the same page. Right now the data is getting stored fine in a model, however, I’m running across a problem because it seems like each time you return to a screen you generate a new instance of the controller. Thus, my model is not of any good because I lose a reference to it (it’s currently stored as an instance variable inside my controller). What am I doing wrong?
Share
You’ll typically have a model (which might be a collection of objects, not necessarily just one) that’s shared across your document or application. When a view controller is created, it’s given a reference to the model (or to some part of the model). If it in turn creates another view controller, it passes on a reference to the model to that object. The model is thus shared by all the view controllers. The model isn’t forgotten when a view controller is deallocated because the other controllers know about it.
It sounds like you’ve got the beginning of a model, but it’s limited to a single view controller. Maybe you’ve got the same situation with some of your other view controllers, too. Think about how you can tie all those small models together into a larger object graph. That’ll make it easier to remember, and it also allows you to put the responsibility for saving the entire model in a single object like your app delegate or root view controller.