title: Accessing and Modifying AppDelegate OUTSIDE Itself
body: <p>In my <strong>viewController</strong>, there's an object of a custom <strong>PolyShape</strong> class. The main variable of it is the number of sides and this modifies everything else in the app.</p>
  
  <p>I'm trying to use the <strong>AppDelegate</strong> and <strong>NSUserDefaults</strong> to store the number of sides on the <strong>NSUserDefault</strong>, but my problem is communicating between the <strong>AppDelegate</strong> and my <strong>viewController</strong>.</p>
  
  <p>I saw that you can make a call to the <strong>AppDelegate</strong> with this line of code
      YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
  which doesn't seem to work for me.</p>
  
  <p>But supposing this DID work and I was able to call the value out of the AppDelegate, how would I write it back in the the app is about to close? Here's my code so far:</p>
  
  <pre><code>@synthesize window = _window;
  @synthesize polySides;
  
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      [self setPolySides: [[NSUserDefaults standardUserDefaults] integerForKey:@"polysides"]];
  
      [self.window makeKeyAndVisible];
      return YES;
  }
  
  - (void)applicationWillResignActive:(UIApplication *)application {
      /*
      Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
      */
      [[NSUserDefaults standardUserDefaults] setInteger: self.polySides forKey:@"polysides"];
  }
  
  - (void)applicationDidEnterBackground:(UIApplication *)application {
      /*
      Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
   If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      */
      [[NSUserDefaults standardUserDefaults] setInteger: self.polySides forKey:@"polysides"];
  }
  
  - (void)applicationWillEnterForeground:(UIApplication *)application
  {
      /*
      Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      */
      [self setPolySides: [[NSUserDefaults standardUserDefaults] integerForKey:@"polysides"]];
  }
  
  - (void)applicationDidBecomeActive:(UIApplication *)application {
      /*
      Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      */
  }
  
  - (void)applicationWillTerminate:(UIApplication *)application {
      /*
      Called when the application is about to terminate.
      Save data if appropriate.
      See also applicationDidEnterBackground:.
      */
      [[NSUserDefaults standardUserDefaults] setInteger:polygon.numberOfSides forKey:@"polysides"];
  }
  
  - (void)dealloc {
      [_window release];
      [super dealloc];
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> I guess I wasn't clear. What I want to be able to do is the following:</p>
  
  <pre><code>[[NSUserDefaults standardUserDefaults] setInteger: polygon.numberOfSides forKey: @"polysides"];
  </code></pre>
  
  <p>where polygon is an object in my controller. Similarly I'd like to be able to do</p>
  
  <pre><code>[polygon.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];
  </code></pre>