2011-10-04 2 views
0

보기 콘트롤에 사용자 정의 PolyShape 클래스의 객체가 있습니다. 그것의 주요 변수는 변의 수이고 이것은 앱의 다른 모든 것을 수정합니다. 외부로 AppDelegate 액세스 및 수정

은 내가 NSUserDefault에면의 수를 저장하기 위해 AppDelegate에NSUserDefaults를 사용하기 위해 노력하고있어,하지만 내 문제는 AppDelegate에의 ViewController 사이에 통신하고 있습니다.

난 당신이 코드 YourAppDelegate * AppDelegate에 = (YourAppDelegate *) [[UIApplication sharedApplication] 위임]이 라인으로 AppDelegate에로 전화를 걸 수있는 것을 보았다; 나를 위해 작동하지 않는 것 같습니다.

하지만이 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"]; 

답변

0

할 수 있도록하고 싶습니다 :

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

, 좋아 유용하지만, 내 질문에 대답하지 않습니다. 내가 원했던 것 (그리고 내가 더 명확해야했음을 짐작할 수있다.) [[NSUserDefaults standardUserDefaults] setInteger : polygon.numberOfSides forKey : @ "polysides"]; 여기서 polygon은 컨트롤러의 개체입니다. 마찬가지로 할 수 있기를 원합니다. [polygon.setNumberOfSides : [[NSUserDefaults standardDefaults] integerForKey : @ "polysides"]; –