A View Controller contains:
UITextField(Name).
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:
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!