2012-10-07 42 views
1

我一直在嘗試沒有任何運氣來實現狀態恢復和保存。iOS 6保存和恢復沒有故事板

我不使用故事板。我必須做錯什麼。

我爲每個Nib文件設置了恢復標識符。

我知道狀態正在存儲,但從來沒有正確恢復。最後一個狀態顯示半秒鐘,然後返回到主視圖。我嘗試了許多事情,但沒有運氣。請幫忙!

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSLog(@"will finish"); 
    return YES; 
} 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 

    navigator = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navigator; 

    [self.window makeKeyAndVisible]; 
    [navigator release]; 

    NSLog(@"did finish"); 
    return YES; 
} 
// As you can see, I started the process of saving and restoring application state. 
// Also, I added the restoration identifier for every class that should be restored. 

-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder 
{ 
    NSLog(@"restoring"); 
    return YES; 
} 
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder 
{ 
    return YES; 
} 
-(void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder 
{ 
    NSLog(@"restored"); 
    //navigator = [[UINavigationController alloc] initWithRootViewController:]; 

    isRestored = YES; 
} 
+0

我還沒有理由去整合狀態保存/恢復,但是我發現你很可能在你的應用程序中重新開始恢復狀態:didFinishLaunchingWithOptions:方法。您的NSLog()在控制檯中顯示的順序是什麼? –

+0

基督徒,謝謝你的回答。該NSLogs順序顯示: 「將完成」 「恢復」 「恢復」 「沒有完成」 我敢肯定,你指出的是真實的。事情是我不知道如何解決它。我嘗試了很多東西。我在iOS中的經驗有限。 – Paradox

+1

我試圖讓它沒有Storyboard而沒有成功。 它看起來像故事板正在照顧很多工作,並且在代碼中進行操作需要您編寫實際的保存代碼。 雖然我可能是錯的。 http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/StatePreservation/StatePreservation.html – sachadso

回答

0

因爲application:didFinishLaunchingWithOptions:調用之前恢復做你應該做你的控制器初始化在application:willFinishLaunchingWithOptions:

檢查this answer

此外,你應該編程爲所有控制器分配restoreIdentifier。