The Man
The Man

Reputation: 1462

Saving iOS Application Scene State

This may be impossible, but I'm trying to save the state of my application between scene transitions, but I can't figure out what to do. Currently I love the way that when you have an application running and hit the home button, you can go back to that application just where you left off, but if you transition between scenes (in a storyboard), once you get back to that scene the application state was not saved.

I only have two different scenes that need to be saved (you transition back and forth from one to the other). How can I go about saving a storyboard scenes state without taking up precise memory?

More Detailed: Here is my entire storyboard. You transition back and forth between scenes using the plus toolbar button. On the second scene the user can tap on the table view cells and a real image will fill the image view (See figure 1.2)

Figure 1.1Figure 1.1

In figure 1.2 you see what happens when you tap inside one of the many table view cells (an image view pops up.)

Figure 1.2 Figure 1.2

THE PROBLEM: When you tap a table view cell, which fills an image view (shown in figure 1.2) it works fine if you stay on that scene or even hit the iPhone home button (if you hit the iPhone home button and then reopen the app the scene's state was saved and the image view filled with a simple image still shows just like we left it), but if I transition (using the plus button) back to the first scene, and then use the plus button on the first scene to get back to the second scene the image view that I created (shown in figure 1.2) disappears and the second scene loads without saving the state and image views we filled.

EDIT: I tried using the same view controller for both scenes, but it didn't solve the problem.

UPDATE: I just found the following code (that I think stores a views state). How could I use this and is this what I've been looking for?

MyViewController *myViewController=[MyViewController alloc] initWithNibName:@"myView" bundle:nil];
[[self navigationController] pushViewController:myViewController animated:YES];
[myViewController release];

Upvotes: 11

Views: 1928

Answers (7)

user1354889
user1354889

Reputation:

I would suggest a combination of two things: 1. Take DBD's advice and make sure that you don't continuously create new views 2. Create a shared class that is the data controller (for the golfers, so that the data is independent of the scene)

The correct way to make the segues would be to have one leading from the view controller on the left to the one on the right. However, to dismiss the one on the right you can use

-(IBAction)buttonPushed:(id)sender 
      [self dismissModalViewControllerAnimated:YES];
}

This will take you back the the view controller on the left, with the view controller on the left in its original state. The problem now is how to save the data on the right.

To do this, you can create a singleton class. Singleton classes have only one instance, so no matter how many times you go to the view controller on the right, the data will always be the same.

Singleton Class Implementation (Of a class called DataManager) - Header

@interface DataManager : NSObject {
}

+(id)initializeData;
-(id)init;
@end

Singleton Class Implementation (Of a class called DataManager) - Main

static DataManager *sharedDataManager = nil;

@implementation DataManager
+(id)initializeData {
    @synchronized(self) {
        if (sharedDataManager == nil)
            sharedDataManager = [[self alloc] init];
    }
    return sharedDataManager;
}

-(id)init { 
    if(self == [super init]) {
    } 
    return self;
}
@end

Then, inside your view controller code you can grab this instance like this

DataManager *sharedDataManager = [DataManager initializeDataManager];

This way you will have the same data no matter how many times you switch views. Also, you can better adhere to MVC programming by keeping you data and your view controllers separate. (http://en.wikipedia.org/wiki/Model–view–controller)

Upvotes: 2

Caleb
Caleb

Reputation: 125017

The key here is to:

  1. Have some sort of storage for the data that your application needs. This is your application's data model.

  2. Give each view controller access to the model, or at least to the part of the model that it needs to do its job. The view controller can then use the data from the model to configure itself when it's created, or when the view is about to appear.

  3. Have each view controller update the model at appropriate times, such as when the view is about to disappear, or even every time the user makes a change.

There are a lot of ways that you can organize your data in memory, and there are a lot of ways that you can store it on disk (that is, in long term storage). Property lists, Core Data, plain old data files, and keyed archives are all possibilities for writing the data to a file. NSArray, NSDictionary, NSSet, and so on are all classes that you can use to help you organize your data in memory. None of that has anything to do with making your view controllers feel persistent, though. You'll use them, sure, but which one you choose really doesn't matter as far as updating your view controllers goes. The important thing, again, is that you have some sort of model, and that your view controllers have access to it.

Typically, the app delegate sets up the model and then passes it along to the view controllers as necessary.

Something else that may help is that you don't have to let your view controller(s) be deleted when they're popped off the navigation stack. You can set up both view controllers in your app delegate, if you want, so that they stick around. You can then use the ones you've got instead of creating new ones all the time, and in so doing you'll automatically get some degree of persistence.

Upvotes: 0

S.H.
S.H.

Reputation: 2853

I don't think you should cycle the segues, just use one that connects viewcontroller 1 from viewcontroller 2 should be enough and that way you make sure that no additional viewcontrollers are being made (memory problems maybe?)

However for your particular problem, I believe that you should use core data to save the exact state of your table, view because ios doesn't save the exact state of view at all times. it will require work but you will achieve what you want. You will need to save the exact photo( using a code or enums that will be saved), the location in the table view, the score or well whatever data you need to save that state.

The best of all is that coredata is so efficient that reloading the data when the app is relaucnhed or into foreground it takes no time, and ive used core data to load more than 5k of records until now and works just fine and its not slow at all.

When i get back home ill provide a code you might use to get an idea of what i mean.

Upvotes: 0

DBD
DBD

Reputation: 23223

Figure 1.1 has a fundamental flaw which I believe the basis of your problem.

Segues (the arrows between controllers on the storyboard) create new versions of the UIViewControllers. You have circular segues. So when you go "back" to the original screen through the segue is really taking you forward by creating a new version.

This can create a major problem for memory usage, but it also means you can't maintain state because each newly created item is an empty slate.

Since your are using a UINavigationController and pushViewController:animated: you should "pop" your controller to get rid of it.

On your "second" scene, remove the segue from the + button and create an IBAction on a touchUpInside event. In the IBAction code add the "pop"

- (IBAction)plusButtonTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 2

TegRa
TegRa

Reputation: 519

Store the state of the scene in NSUserDefaults or inside a plist file then when loading up the scene just load it with the settings from there. If the images are loaded from the internet you might also want to save them locally on your iphones hard drive so it runs a bit smoother.

Upvotes: 0

Patrick Tescher
Patrick Tescher

Reputation: 3447

Apple isn't going to do this for you. You should probably just save the state of each view using NSUserDefaults and each time your application launches re-load your saved data.

If you are storing everything in CoreData you would only need to save the active view and a few object ids, if not you would need to save any data you have.

Don't expect iOS to save anything that you have in memory between launches. Just store it in NSUserDefaults and load it each time.

Upvotes: 0

CodaFi
CodaFi

Reputation: 43330

I see what you mean. This should happen to every application, as when the last view controller in the navigation stack is transitioned away from, it is deallocated and freed. If you need to save values such as text or object positions, a plist may be the way to go. See this related question for how to use a plist.

Upvotes: 0

Related Questions