在外部访问和修改 AppDelegate
在我的 viewController 中,有一个自定义 PolyShape 类的对象。它的主要变量是边数,这会修改应用程序中的其他所有内容。
我尝试使用 AppDelegate 和 NSUserDefaults 来存储 NSUserDefault 上的边数,但我的问题是 NSUserDefaults 之间的通信>AppDelegate 和我的 viewController。
我看到你可以用这行代码调用 AppDelegate YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 这似乎对我不起作用。
但是假设这个确实有效并且我能够从 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.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];
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 an object in my controller. Similarly I'd like to be able to do
[polygon.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完成一组后,您需要同步默认值:
You need to synchronize your defaults after you do a set: