3

I have some data that's been stored using NSUserDefaults in one view and then being displayed in another view. The issue I'm having is that when the user changes the data and then returns to the view where the data is displayed (in a UILabel), the data that was first saved is displayed instead of the newer saved text.

I think I need to do something with viewDidAppear perhaps, so that every time the view appears the newest saved data is displayed.

here's the code that Im displaying the NSUserDefaults stored info on a UILabel:

   NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTextFieldKey"];
    NSLog(@"Value from standardUserDefaults: %@", aValue);

    NSLog(@"Label: %@", myLabel);
    myLabel.text = aValue;

if someone could point me in the right direction that would be great,

thanks

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

4 Answers4

11

Put this text in

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTextFieldKey"];
    NSLog(@"Value from standardUserDefaults: %@", aValue);

    NSLog(@"Label: %@", myLabel);
    myLabel.text = aValue;
}

And in your "edit" view in - viewWillDisappear: save changes in NSUserDefaults

3

When saving data to NSUserDefaults, it doesnt immediately write to the Persistent Storage. When you save data in NSUserDefaults, make sure you call:

[[NSUserDefaults standardUserDefaults] synchronize];

This way, the value(s) saved will immediately be written to Storage and each subsequent read from the UserDefaults will yield the updated value.

Ameer Sheikh
  • 770
  • 5
  • 14
  • 1
    the second part of the second part is not true. NSUserDefaults always reads from its cache first. And if you save something it is written to the cache immediately. The need for `synchronize` after each write to NSUserDefaults is an urban myth among iPhone developers. – Matthias Bauch Sep 26 '11 at 11:22
2

Do not forget the use synchronize when you set some value

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:YOUR_VALUE forKey:@"KEY_NAME"];
[defaults synchronize];

Now you can place your code in viewWillAppear method to retrieve the value from defaults, this will help you fetch the currentsaved value for your desired key.

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* strValue =    [defaults objectForKey:@"KEY_NAME"];
myLabel.text = strValue != nil ? strValue : @"No Value";

Hope it helps

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Rahul Sharma
  • 3,013
  • 1
  • 20
  • 47
0

In Swift we can do it following way:

To save value:

 let defaults = UserDefaults.standard
 defaults.set(YOUR_VALUE, forKey: "YOUR_KEY_NAME")

To get value:

 let defaults = UserDefaults.standard
 let data = defaults.objectForKey("YOUR_KEY_NAME")

For more details visit: https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults

H S W
  • 6,310
  • 4
  • 25
  • 36