0

I am currently developing a multi-view app with storyboards. In each view the user can enter data, generally through text boxes or button entry. This data is then stored in arrays.

My problem is this: when I transfer back to a window, viewDidLoad is called, and therefore resets all of the data. Is there a simple way (without simply loading my arrays back up) to retain this data, as if the viewDidLoad was not called? Is there a method that is called when the application is loaded, as opposed to just the view? Or perhaps a way to only call the viewDidLoad the first time?

I am aware I can solve this by loading my arrays back, I was just wondering if there's a simpler, more elegant solution.

EDIT:

It appears my view did load is triggering every time I go to another view. I am using storyboards segues.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tony Hematite
  • 149
  • 3
  • 14
  • Store whether it has been loaded before in core data and check that in viewdidload – AndyOS Apr 28 '14 at 12:04
  • I've never used core data before. How would I do this? – Tony Hematite Apr 28 '14 at 12:06
  • 1
    If your data structure is simple you can use `NSUserDefaults` or plists. – Rukshan Apr 28 '14 at 12:08
  • 1
    If you need to access your array often and it's not huge, you could create a singleton manager for it. (Given that the content doesn't need to be persistent) – Lord Zsolt Apr 28 '14 at 12:08
  • If you are going BACK to a previous view, then the viewDidLoad will not call. Only viewWillAppear will be called. Not sure why viewDidLoad is being called in your case. Can you include the code you have written when going back from a view controller? – iAmd Apr 28 '14 at 12:10
  • 1
    ViewDidLoad is exactly the function you are looking for. Unlike e.g. ViewDidAppear, it is supposed to be called only once, when your view is loaded for the first time. So probably either you are creating a new instance of the view (e.g. if you invoke another segue to it rather than unwinding or popping), or your view gets unloaded from memory for other reasons. – user1459524 Apr 28 '14 at 12:13
  • @user1459524, I believe you are right here. It appears that the 'viewDidLoad' is calling whenever I go to a view. Unsure as to why this is. I am using simple modal segues in storyboard. – Tony Hematite Apr 28 '14 at 12:24
  • Each time you use a segue, you create a new instance of the destination view controller. If you want to go BACK to a previous VC, use "unwind" segues or dismissViewController:animated, Or a navigation controller. Now that may not solve your immediate issue here, depending on what is retained from the previous view (e.g via strong properties) but it's definitely something that you should fix to avoid issues down the road I'd say. – user1459524 Apr 28 '14 at 12:40

4 Answers4

2

You may use NSUserDefaults. in viewdidload check if you have data then load data or display default values. Every time a viewloaded viewDidLoad is called.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
1

You can make the sub ViewControllers properties of the root, that way they won't be deallocated as you move between them. With the obvious caveat that when the app is killed data is lost.

For permanent storage of array data I suggest looking into NSCoding.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
0

If anyone is interested in seeing how I solved this, I've used p-lists. It's quite a workaround, but the data is required to be in a p-list anyway, so this wasn't much of a stretch for my requirements:

First:

Ensure that the code that we want to run once at the beginning runs once:

 static dispatch_once_t once;
dispatch_once(&once, ^ { 
//all code we want to run once goes here
}

Secondly, when the view disappears write all required data into plist:

 (void) viewDidDisappear:(BOOL)animated
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [path objectAtIndex:0];
filePath = [documentFolder stringByAppendingFormat:@"wantedData.plist"];
[wantedDataArray writeToFile:filePath atomically:YES];
}

Finally, when view loads (or appears) fill an array with the contents of the p-list:

    retrievedDataArray = [NSArray arrayWithContentsOfFile:filePath];
Tony Hematite
  • 149
  • 3
  • 14
  • 1
    It's OK to save the data to a file if you need the data to persist between launches of the app, but you need to understand why you had the problem in the first place, and that was caused by using segues (that were not unwind segues) to "go back" to previous views as @user1459524 said in the comments. – rdelmar Apr 28 '14 at 16:30
  • that is so accurate ! Tony, you gotta listen to RD on this one. – Fattie May 24 '14 at 20:44
  • (I was going to humorously suggest you just keep the data on parse.com ... but that's sort of what you've done here!) BTW if you are in a muddle about "sub containers", here's my XXX-long explanation http://stackoverflow.com/a/23403979/294884 – Fattie May 24 '14 at 20:45
0

From the comments I've gathered that the best way to do this is using an unwind segue. This can be done as follows:

Insert IBAction in segue I want to unwind to:

.h

- (IBAction)unwindToMain:(UIStoryboardSegue *)unwindSegue;

.m

- (IBAction)unwindToMain:(UIStoryboardSegue *)unwindSegue
{
    //Allows us to unwind back to this segue
}

Then simply ctrl dragging between the button in my second view controller and selecting the segue.

It is possible to find a good tutorial on this here:

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Tony Hematite
  • 149
  • 3
  • 14