在自己的外部访问和修改AppDelegate

时间:2011-10-04 22:13:03

标签: cocoa-touch uiviewcontroller uiapplicationdelegate

在我的 viewController 中,有一个自定义 PolyShape 类的对象。它的主要变量是边数,这会修改应用程序中的其他所有内容。

我正在尝试使用 AppDelegate NSUserDefaults 来存储 NSUserDefault 上的边数,但我的问题是在 AppDelegate 和我的 viewController

我看到你可以用这行代码调用 AppDelegate     YourAppDelegate * appDelegate =(YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 这似乎对我不起作用。

但是假设这个DID工作并且我能够从AppDelegate中调用该值,那么我将如何在应用程序中将其写回来关闭?到目前为止,这是我的代码:

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

编辑:我想我不清楚。我希望能够做到的是:

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

其中polygon是我控制器中的一个对象。同样,我希望能够做到

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

1 个答案:

答案 0 :(得分:0)

您需要在执行设置后同步默认值:

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