开发者

Accessing and Modifying AppDelegate OUTSIDE Itself

In my viewController, there's an object of a custom PolyShape class. The main variable of it is the number of sides and this modifies everything else in the app.

I'm trying to use the AppDelegate and NSUserDefaults to store the number of sides on the NSUserDefault, but my problem is communicating between the AppDelegate and my viewController.

I saw that you can make a call to the AppDelegate with this line of code YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; which doesn't seem to work for me.

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:

@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];
}

EDIT: I guess I wasn't clear. What I want to be able to do is the following:

[[NSUserDefaults standardUserDefaults] setInteger: polygon.numberOfSides forKey: @"polysides"];

where polygon is a开发者_开发百科n object in my controller. Similarly I'd like to be able to do

[polygon.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];


You need to synchronize your defaults after you do a set:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger: self.polySides forKey:@"polysides"];
[defaults synchronize];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜