0

I recently figured out with some help here how to restore my text fields, button states, etc. when putting my app into the background, terminating, and restarting.

What I am now trying to figure out is how to keep the same information when going back to my main menu using the back arrow in the navigation bar. Of course, when I have filled out info on my sub view, hit the back button to go to the main menu, all of my user fields and button states are reset to their initial state.

I am not sure what code you might want to see here. I am happy to provide anything, but I am not quite sure what would be relevant.

My app set up is very simple. It looks like this:

--->navigation controller---->main menu----->calculator

Main menu is embedded in the navigation controller. I just need to be able to go back to the main menu from the calculator without losing the data the user has entered into the fields in th calculator.

Thanks in advance.

EDIT - This is what I am using to save and restore data for general state preservation / restoration - code added:

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    [coder encodeObject:_startLevel.text forKey:@"startText"];
    [super encodeRestorableStateWithCoder:coder];
}

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    // start level text
    _startLevel.text = [coder decodeObjectForKey:@"startText"];
    [super decodeRestorableStateWithCoder:coder];
}

2 Answers 2

1

There are multiple ways to store/restore data..

  1. You can make a Singleton class and store/retrieve data in/from that.
  2. You can store/retrieve data in/from NSUserDefaults
  3. You can store/retrieve data in/from databases e.g., sqlite, CoreData
  4. You can store/retrieve data in/from files like text files, plist. files.

It's up to you, what suits you the best. However, you can save data in either viewDidUnload or viewWillDisappear and retrieve that in either viewDidLoad or viewWillAppear of your Calculator View Controller.

2
  • I know I am going to want to use viewWillDisappear and viewWillAppear since I already have an initialization routine that sets all of my fields / buttons / controls on viewDidLoad. I guess I am looking for the most straight forward, common way of doing this. I have to save 5 button states, 2 text fields, and a few labels. Do you think NSUserDefaults would be the way to go?
    – C_Dub
    Feb 14, 2014 at 5:48
  • You can also use Singleton object if you don't want to restore view controller's state after app relaunch.. Feb 14, 2014 at 6:50
0

I recently figured out with some help here how to restore my text fields, button states, etc. when putting my app into the background, terminating, and restarting.

So it seems like you have a mechanism for storing and retrieving data.

Now when you hit back to mainmenu, you can just save the data in viewWillDisappear delegate and restore data in viewWillAppear method of calculator controller.

Hope this helps.

7
  • This is what I am using right now in my calculator view controller implementation file. There are a bunch of these encoder/decoder pairs, but you will get the gist. Do I need to copy all of that encoder data in the viewWillAppear and then the decoder code in viewWillAppear? I will put my code in the OP because it isn't going to fit here due to character limit.
    – C_Dub
    Feb 14, 2014 at 4:27
  • Yes. You need to save the data before leaving to mainmenu.
    – HRM
    Feb 14, 2014 at 4:42
  • This is producing undeclared identifier / coder when in my calculator's implementation file.`- (void)viewWillDisappear:(BOOL)animated { // start level text [coder encodeObject:_startLevel.text forKey:@"startText"]; [super encodeRestorableStateWithCoder:coder]; } - (void)viewWillAppear:(BOOL)animated { // start level text _startLevel.text = [coder decodeObjectForKey:@"startText"]; [super decodeRestorableStateWithCoder:coder]; }'
    – C_Dub
    Feb 14, 2014 at 5:44
  • You should use coder and decoder same way as you did when you put app in background.
    – HRM
    Feb 14, 2014 at 5:51
  • Oh man. I feel like I am so close. Am I putting the code in the wrong place? I am putting it in my implementation file below viewDidLoad, and I am getting the 'undeclared identifier 'coder' error. I appreciate your help on this.
    – C_Dub
    Feb 14, 2014 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.