在自身外部访问和修改 AppDelegate

Posted

技术标签:

【中文标题】在自身外部访问和修改 AppDelegate【英文标题】:Accessing and Modifying AppDelegate OUTSIDE Itself 【发布时间】:2011-10-04 22:13:02 【问题描述】:

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

我正在尝试使用 AppDelegateNSUserDefaults 来存储 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.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];

【问题讨论】:

【参考方案1】:

你需要在设置后同步你的默认值:

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

【讨论】:

好的,有用,但没有回答我的问题。我想要的(我想我应该更清楚)能够做到 [[NSUserDefaults standardUserDefaults] setInteger: polygon.numberOfSides forKey: @"polysides"];其中多边形是我的控制器中的一个对象。同样,我希望能够做到 [polygon.setNumberOfSides: [[NSUserDefaults standardDefaults] integerForKey: @"polysides"];

以上是关于在自身外部访问和修改 AppDelegate的主要内容,如果未能解决你的问题,请参考以下文章

使用外部 Array 方法修改 Array 实例自身

在 C++ 中从外部访问嵌套类

从 AppDelegate 外部调用放置在 AppDelegate 中的 UIActionSheet

面向对象特征

在静态方法中new 一个内部类对象和new 一个外部类对象的区别

Android 11 拍照+录制视频保存到外部共享区域