Question

A View Controller contains:

  1. UITextField(Name).

  2. UIButton(save).

Once I type any name and hit save, The name is added to an array which is stored using NSUserDefaults.

This View Controller is pushed from another View Controller and so when I go and come back to this View Controller, the contents of NSArray gets overwritten with the new names that i might specify. I'm not sure whats causing this but i'm suspecting that it has to do something with allocation and initialization of NSArray.

In my ViewDidLoad, I did NSMutableArray *namesList= [[NSMutableArray alloc]init];

And in my OnSave method,

[namesList addObject:nameTextField.text];
[[NSUserDefaults standardUserDefaults] setObject:namesList forKey:@"namesListArray"];
NSLog(@"items in array: %@",namesArray);

EDIT: Screenshot of related View Controllers:

enter image description here

Here, when i click the ADD button, the second View Controller is pushed. When i enter "kenneth" and hit save, the console says:

2014-03-03 11:03:46.219 SmartWatch[664:a0b] items in array: (
kenneth)

Now, without going back to the previous View Controller ,when i type another name say,"john" and hit save, the console says:

2014-03-03 11:15:43.646 SmartWatch[664:a0b] items in array: (
kenneth,
john)

Until now everything is good. The problem occurs when i go back to the previous Controller and come back to the second View Controller.Now, when i type another name ,say "scott",the console says:

2014-03-03 11:19:50.815 SmartWatch[664:a0b] items in array: (
scott) 

This means that the previous two names were overwritten. I want the array to retain "kenneth" and "john" too and "scott" should actually be appended to the array.

I think this happens because the array gets initialized when the second View Controller comes to the foreground again but I'm not sure of it and i dont know how to rectify this.

Hope the question is clear now. Thanks!

Was it helpful?

Solution

Try this,

NSArray *nameArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"namesListArray"];
NSMutableArray * namesList;
if (!nameArray) {
    namesList = [[NSMutableArray alloc] init];
} else {
    namesList = [NSMutableArray arrayWithArray:nameArray];

}

On save

[namesList addObject:nameTextField.text];
[[NSUserDefaults standardUserDefaults] setObject: namesList forKey:@"namesListArray"];
[[NSUserDefaults standardUserDefaults] synchronize];

OTHER TIPS

Make your nameList array as a property and in viewDidLoad

self.namesList = [[[NSUserDefaults standardUserDefaults] objectForKey:@"namesListArray"] mutableCopy];


 if (nil == namesList) 
    {
        self.namesList = [[NSMutableArray alloc]init];
    }

onSave method

[self.namesList addObject:nameTextField.text];
[[NSUserDefaults standardUserDefaults] setObject:self.namesList forKey:@"namesListArray"];

Whether you are doing any functionality to store elements to nsarray in ViewWillAppear Method.
If not please specify your code properly to help!

Add [[NSUserDefaults standardUserDefaults] synchronize]; at end of NSUserDefaults. Other wise problem is else where, you need to to put relevant code with output of NSUserDefaults.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top