-1

I have a UILabel called 'labelA'. In this label is the letter 'A'.

When I am using NSUserDefault and start my app, the label is empty, but I don't know why?

Here is my ViewController.m file:

- (void)viewDidLoad {

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

self->labelA.text = [defaults objectForKey:@"labelAtext"]; }


- (void)defaults {

 NSString *textLabelA = labelA.text;

 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:textLabelA forKey:@"labelAtext"];
[defaults synchronize];
}

Here is my NSString in the ViewController.h file

NSString *textLabelA =@"A";

When i load my app without NSUserDefault the labelA.text = A. does anybody know what's my problem. Thanks a lot :-)

Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72
euleec7
  • 55
  • 1
  • 6

2 Answers2

0

Your method defaults is not being called anywhere, so the text value is never stored in NSUserDefaults.

[self defaults]; // This needs to be called somewhere before you try and access NSUserDefaults.

You should also be accessing self using dot notation, not ->

self.labelA.text = [defaults objectForKey:@"labelAText"];

-> is for dereferencing a struct in C, for more information - What is "->" in Objective C?

Community
  • 1
  • 1
Tim
  • 8,932
  • 4
  • 43
  • 64
  • if i use `self.labelA.text = [defaults objectForKey:@"labelAText"];` i get an error. property 'labelA' not found on object of type "ViewController"; did you mean to acces instance variable 'labelA'? – euleec7 Feb 25 '15 at 18:08
  • should i call `[self defaults]; ` in `- (void)viewDidLoad {}` ? – euleec7 Feb 25 '15 at 18:11
  • Wherever you want, so long as the value is set in NSUserDefaults before you try and read NSUserDefaults. It's hard to tell exactly what you want from your question. – Tim Feb 26 '15 at 09:07
  • what i want is. that my text changes in all my labels l will not change when my app goes to background. – euleec7 Feb 26 '15 at 09:33
  • So on load you need to check if a value exists in NSUserDefaults, if it does, display, it doesn't set the label to whatever your default value is. Whenever your UILabel text value changes during the running of the app, update your NSUserDefaults with the new value. – Tim Feb 26 '15 at 09:53
0

When you use UserDefaults. You should first test if you get a value as if the value doesnt exist you need to set it or not.

 NSString * myPreference = @"labelAtext";

- (void)viewDidLoad {
   NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
   id labelValue = [userDefault objectForKey:myPreference];
   if (labelValue)      // if preference exist
        self.labelA.text = labelValue; // set ur label
   else
        [self storeDefaultValue]; // you store a default value
 }

- (void)storeDefaultValue 
{
 NSString *myDefaultValue = nil;
 if ([self.labelA.text isNotEqualToString:@""]) // if ur label not empty
      myDefaultValue = self.labelA.text;
 else 
      myDefaultValue = @"Not Available"; // or whatever defaults value you want
 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
 [defaults setObject:myDefaultValue forKey:myPreference];
 [defaults synchronize];
}

What do you really want to achieve as I don't really understand whats the point of storing a value in preference as soon as you load your app. Give more details on what is your goal.

Benpaper
  • 144
  • 4
  • ok, i will explain what i want´t to do. i have a "letter random" which shows with every button "generateLetter" a new letter in in a label "labelShowLetter". for every letter from a-z i have an extra label (like labelA, labelB ....). if labelShowLetter.text isEqualTo labelA.text then labelA.text =@““; for this i need the NSUserDefault...i hope this will help you? – euleec7 Feb 25 '15 at 20:32
  • Forget about label, user default, function name and all technical terms, and explain simply the goal of your code. I think you don't need UserPreference if I understand well. NSUserDefaults is dedicated to store User Preference such as favorite colour, config specific to a user more like a config parameter relative to a specific user. https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html – Benpaper Feb 25 '15 at 23:52
  • ahh ok. what i want is. that my text changes in my label will not change when my app goes to background. – euleec7 Feb 26 '15 at 06:41