title: Can't read NSUserDefaults data between views
body: <p>Disclaimer: major noob</p>
  
  <p>I'm writing an arithmetic flash card app as a learning project. I have a UITabViewController with the bottom tab bar that toggles between a few different views. Everything works okay until I try to set NSUserDefault boolean values in the Settings view controller and try to read those values in the Flashcards view controller. </p>
  
  <p>The settings view has a switch to enable/disable each operator (addition, subtraction, etc) and the flashcard view should randomly present a flash card if that type of operation was enabled.</p>
  
  <p>I believe that my mistake is that I don't understand the key concept of data encapsulation, objects, etc. I'd appreciate any help.</p>
  
  <p>Here is the Settings view controller. I'm not even sure how to put the code into this forum so this might be a laughable moment... here goes:</p>
  
  <pre><code>//  settings_flashcards.h
  
  #import &lt;UIKit/UIKit.h&gt;
  @interface settings_flashcards : UIViewController {
  UISwitch *additionSwitch;
  UISwitch *subtractionSwitch;
  UISwitch *multiplicationSwitch;
  UISwitch *divisionSwitch;
  }
  
  @property (nonatomic,retain) IBOutlet UISwitch *additionSwitch;
  @property (nonatomic,retain) IBOutlet UISwitch *subtractionSwitch;
  @property (nonatomic,retain) IBOutlet UISwitch *multiplicationSwitch;
  @property (nonatomic,retain) IBOutlet UISwitch *divisionSwitch;
  
  @end
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>/  settings_flashcards.m
  
  #import "settings_flashcards.h"
  
  @implementation settings_flashcards
  
  @synthesize additionSwitch;
  @synthesize subtractionSwitch;
  @synthesize multiplicationSwitch;
  @synthesize divisionSwitch;
  
  - (void)viewDidLoad {
  
  [additionSwitch addTarget:self action:@selector(additionSwitchFlipped) forControlEvents:UIControlEventValueChanged];
  [subtractionSwitch addTarget:self action:@selector(subtractionSwitchFlipped) forControlEvents:UIControlEventValueChanged];
  [multiplicationSwitch addTarget:self action:@selector(multiplicationSwitchFlipped) forControlEvents:UIControlEventValueChanged];
  [divisionSwitch addTarget:self action:@selector(divisionSwitchFlipped) forControlEvents:UIControlEventValueChanged];
  [super viewDidLoad];
  }
  
  -(void) additionSwitchFlipped {
  if (additionSwitch.on) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"additionKey"];
  }else {
      [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"additionKey"];
  }
  }
  
  -(void) subtractionSwitchFlipped {
  if (subtractionSwitch.on) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"subtractionKey"];  
  }else {
      [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"subtractionKey"]; 
  }
  }
  
  -(void) multiplicationSwitchFlipped {
  if (multiplicationSwitch.on) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"multiplicationKey"];   
  }else {
      [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"multiplicationKey"];  
  }
  
  }
  
  -(void) divisionSwitchFlipped {
  if (divisionSwitch.on) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"divisionKey"]; 
  }else {
      [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"divisionKey"];    
  }
  }
  </code></pre>
  
  <p>Here is the Flashcards view...</p>
  
  <pre><code>//  flashcardsViewController.h
  
  #import &lt;UIKit/UIKit.h&gt;
  
  @interface flashcardsViewController : UIViewController &lt;UIActionSheetDelegate&gt;{
  
  UILabel *firstNumberLabel;
  UILabel *secondNumberLabel;
  UILabel *answerNumberLabel;
  UILabel *operatorLabel;
  
  BOOL additionIsEnabled;
  BOOL subtractionIsEnabled;
  BOOL multiplicationIsEnabled;
  BOOL divisionIsEnabled;
  }
  
  @property (nonatomic,retain) IBOutlet UILabel *firstNumberLabel;
  @property (nonatomic,retain) IBOutlet UILabel *secondNumberLabel;
  @property (nonatomic,retain) IBOutlet UILabel *answerNumberLabel;
  @property (nonatomic,retain) IBOutlet UILabel *operatorLabel;
  
  -(void) buttonClicked:(id)sender;
  
  @end
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>//  flashcardsViewController.m
  
  #import "flashcardsViewController.h"
  
  @implementation flashcardsViewController
  
  @synthesize firstNumberLabel;
  @synthesize secondNumberLabel;
  @synthesize answerNumberLabel;
  @synthesize operatorLabel;
  
  - (void)viewDidLoad {
  
  srand(time(0)); //seed random
  
  //the following should assign the keys if they don't exist  
  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"additionKey"]){
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"additionKey"];
  }   
  
  
  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"subtractionKey"]) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"subtractionKey"];
  }
  
  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"multiplicationKey"]) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"multiplicationKey"];
  
  }
  
  if (![[NSUserDefaults standardUserDefaults] boolForKey:@"divisionKey"]) {
      [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"divisionKey"];
  }
  
  //the following should assign each BOOL variable based on the key
  additionIsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"additionKey"];
  subtractionIsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"subtractionKey"];
  multiplicationIsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"multiplicationKey"];
  divisionIsEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"divisionKey"];
  [super viewDidLoad];
  }
  
  -(void) buttonClicked:(id)sender{
  
  int a = rand() %  4;// random number generator (number to enter loop)   
  
  if ( additionIsEnabled || subtractionIsEnabled || multiplicationIsEnabled || divisionIsEnabled) {
      while (a &lt; 5) {
          switch (a) {
              case 0:
                  if (additionIsEnabled == TRUE){
                      int x = rand() % 11 + 1;
                      int y = rand() %11 + 1;
                      firstNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",x];
                      secondNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",y];
                      answerNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",(x+y)];
                      operatorLabel.text = [[NSString alloc]initWithFormat: @"+"];
                      a = 5;
                  }
                  else a++;
                  break;
              case 1:
                  if (subtractionIsEnabled == TRUE){
                      int x = rand() % 19 + 1;
                      int y = rand() %11 + 1;
                      firstNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",x];
                      secondNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",y];
                      answerNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",(x-y) ];
                      operatorLabel.text = [[NSString alloc]initWithFormat: @"-"];
                      a = 5;
                  }
                  else a++;
                  break;
              case 2:
                  if (multiplicationIsEnabled == TRUE){
                      int x = rand() % 11 + 1;
                      int y = rand() %11 + 1;
                      firstNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",x];
                      secondNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",y];
                      answerNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",(x*y)];
                      operatorLabel.text = [[NSString alloc]initWithFormat: @"×"];
                      a = 5;
                  }
                  else a++;
                  break;
              case 3:
                  if (divisionIsEnabled == TRUE){
                      int x = rand() % 11 + 1;
                      int y = rand() % 11 + 1;
                      firstNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",(x*y)];
                      secondNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",y];
                      answerNumberLabel.text = [[NSString alloc]initWithFormat: @"%i",x];
                      operatorLabel.text = [[NSString alloc]initWithFormat: @"÷"];
                      a = 5;
                  }
                  else a = 0;
                  break;
                  default:
                  break;
          }
      }       
  }
  else
  {
      UIAlertView *noOperatorSelectedAlert = [[UIAlertView alloc]initWithTitle:@"You have not set any operations."
                                                     message:@"Return to the settings menu and decide which operations you wish to perform. (addition, subtraction, etc.)"
                                                     delegate:self
                                                     cancelButtonTitle:@"Ok" 
                                                     otherButtonTitles:nil];
      [noOperatorSelectedAlert show];
      [noOperatorSelectedAlert release];
  }
  }
  </code></pre>