简体   繁体   中英

How to save/restore view state in iOS?

I'm using the ECSlidingViewController to create a slide out menu (like Facebook).

I have this storyboard:

故事板

As you can see, I have a navigation controller. I have a major problem though, that even the official demo created by the user who made that controller didn't implement: it doesn't save the state of the view controller when changing views and then coming back .

So, for example, the orange view will always be the first view when I open the app, it gets viewDidLoad . Then I switch to my green view (the second one), and click the button. It changes the background color of that view to red. Then if I go back to my first view, and then back to the second one, the background color of the latter is green again. I want it to stay red.

This is my code to switch views (in MenuViewController ):

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get identifier from selected
    NSString *identifier = [NSString stringWithFormat:@"%@", [self.menu objectAtIndex:indexPath.row]];

    // Add the selected view to the top view
    UIViewController *newTopVC = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

    // Present it 
    [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{        
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = newTopVC;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];

    }];
}

As you can see, it's always instantiating a new VC every time. I want it to save the VC, create the new one if it's not created, then show that one. If the user goes back to a view that has already been created, it should just restore the saved view, not create a new one.

I have put the Init View Controller in a Navigation Controller, now how can I implement this save/restore mechanism for my views? I'd like it to work with 2,3,4, etc....as many views as possible.

Thanks.

When you go "back" you essentially pop the viewcontroller from the navigationstack. At that point there are no more references to that viewcontroller and it get's deallocated and as a result you loose all your changes.

You can handle this a couple of ways:

1) Keep a reference to red/green viewcontroller alive in the parent viewcontroller (the one that is presenting) and use that instead of instantiating a new one. This is not very memory friendly but can be used if used sparingly.

in the interface put:

@property (nonatomic, strong) UIViewController* myGreenController;

then change instantiation to

if (!self.myGreenController)
{
   self.myGreenController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
}
...
self.slidingViewController.topViewController = self.myGreenController;

2) Ideally implement a delegate pattern to pass state back to the parent viewcontroller (something like How do I set up a simple delegate to communicate between two view controllers? ). Then next time when you need the viewController you set the state before presenting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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