2011-10-04 43 views
0

在我的viewController,有一個自定義PolyShape類的對象。它的主要變量是邊的數量,並修改了應用程序中的其他所有內容。訪問和修改AppDelegate本身

我試圖使用的AppDelegateNSUserDefaults的存儲在NSUserDefault邊數,但我的問題是的AppDelegate的viewController之間的通信。

我看到你可以對AppDelegate中打電話與這行代碼 YourAppDelegate *的appDelegate =(YourAppDelegate *)[[UIApplication的sharedApplication]代表]; 這似乎不適合我。

但是,假設這個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"]; 

回答

0

你需要你做一組後,同步您的默認設置:

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

好的,有用的,但沒有回答我的問題。我想要的(我想我應該已經更清楚了)是能夠做 [[NSUserDefaults standardUserDefaults] setInteger:polygon.numberOfSides forKey:@「polysides」]; 其中多邊形是我的控制器中的一個對象。 同樣,我想能夠做 [polygon.setNumberOfSides:[[NSUserDefaults standardDefaults] integerForKey:@「polysides」]; –