Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 在AppDelegate外部访问和修改AppDelegate_Cocoa Touch_Uiviewcontroller_Uiapplicationdelegate - Fatal编程技术网

Cocoa touch 在AppDelegate外部访问和修改AppDelegate

Cocoa touch 在AppDelegate外部访问和修改AppDelegate,cocoa-touch,uiviewcontroller,uiapplicationdelegate,Cocoa Touch,Uiviewcontroller,Uiapplicationdelegate,在我的viewController中,有一个自定义PolyShape类的对象。它的主要变量是边数,这会修改应用程序中的所有其他内容 我试图使用AppDelegate和NSUserDefaults存储NSUserDefaults上的边数,但我的问题是AppDelegate和我的viewController之间的通信 我看到您可以使用这行代码调用AppDelegate YourAppDelegate*appDelegate=(YourAppDelegate*)[[UIApplication shar

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

我试图使用AppDelegateNSUserDefaults存储NSUserDefaults上的边数,但我的问题是AppDelegate和我的viewController之间的通信

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

但是,假设这确实有效,并且我能够从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"];

执行以下设置后,需要同步默认设置:

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

好的,有用,但没有回答我的问题。我想要的(我想我应该更清楚)是能够做[[NSUserDefaults standardUserDefaults]setInteger:polygon.numberOfSides-forKey:@“polysides”];其中多边形是控制器中的对象。类似地,我希望能够执行[polygon.setNumberOfSides:[[NSUserDefaults standardDefaults]IntegerWorkey:@“polysides”];