4

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?

Nosrettap
  • 10,940
  • 23
  • 85
  • 140

4 Answers4

3

What am I doing wrong?

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

It sounds like you need to persist your model data to disk, probably using NSUserDefaults, which is the most appropriate storage mechanism for small amounts of data like this.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • Should my controllers be recreated everytime? My storyboard just has a circle of arrows which allows the user to navigate in a circle. – Nosrettap May 15 '12 at 20:43
  • @nosrettap, that's your problem. You should not be pushing another instance of your original controller onto the stack. You should be popping the others off, to get back to the original instance of your original controller. – Rob May 15 '12 at 22:32
  • See this [SO discussion](http://stackoverflow.com/questions/10544444/memory-not-releasing-with-arc-and-storyboard-in-ios-5-1/10564901#10564901) about the right way to pop view controllers off your stack. – Rob May 15 '12 at 22:34
0

There are at least the following few options you could consider:

  1. As Andrew said, persist the data to disk via NSUserDefaults.
  2. Use a singleton design pattern to create a data "manager" object that will persist the data for the lifetime of the application.
  3. Attempt to find out why a new instance of your view controller is being created (which may be obvious or not so obvious given your code) and attempt to do things a bit differently to reuse the view controller (which may be simple or difficult given your code).

Given that you are using the storyboard described by your comment, I would consider option 2, unless what you really want to do is change the navigation pattern of your view controllers.

Matt
  • 1,586
  • 8
  • 12
  • If I push a controller onto the navigation controller stack, will it persist or will a new instance be created when I pop it off? – Nosrettap May 15 '12 at 20:52
  • 1
    1) Why user defaults? We have no idea how much data there is. The defaults system is easy to use, but many would consider using it this way to be an abuse. 2) Boo. 3) There's nothing at all abnormal about releasing a controller when you're done with it, and no reason to keep it around. Persisting the model is important; the view controllers, no. – Caleb May 15 '12 at 20:54
  • @Nosrettap, when you push a controller on the stack, the old one persists. When you pop the last one off the stack, the old one still persists (but clearly the one you popped off is now gone, so any of it's data will be gone unless you store it in a persistent store (or a singleton)). – Rob May 15 '12 at 22:30
  • @Caleb 1) Sure, for this usage it doesn't seem to be a very good option. 2) Singletons have their place and can be advantageous when the alternative of passing a reference around would require passing various combinations of references to numerous view controllers from code that might not otherwise need that reference ... would you want to pass a reference to an NSNotificationCenter to every object that needed to use it? 3) I agree. The only time a view controller needs to persist is to maintain the state of it's contained views/controls when that data isn't/shouldn't be in a model. – Matt May 16 '12 at 18:24
0

When you push a view controller onto navigation and then come back to it, it persists. But still if you want some data to persist across several screens, singleton instances is a good idea.

zahreelay
  • 1,742
  • 12
  • 18