I want to access a NSString from AppDelegate.m that is located inside ViewController.m.
I have a Single View Application, and i want to save my NSString using applicationDidEnterBackground: inside AppDelegate.m.
The NSString is located inside ViewController.m, and is not declared in AppDelegate.m.
I tried to declare it in AppDelegate.h and then access it in the ViewController.m (And reversed).
ViewController.h:
@interface MyAppViewController : UIViewController {
NSString *MyString;
}
AppDelegate.m:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:MyString forKey:@"SavedString"];
[defaults synchronize];
}
You can register MyAppViewController to observer UIApplicationDidEnterBackgroundNotification like this:
and in the method
goToBackgroundyou can save in NSUserDefaults theMyString.