2013-06-03 7 views
2

스토리 보드를 사용하지 않는 iOS 6 앱에서 상태 보존 및 복원을 구현하고 싶습니다. 주보기 컨트롤러는 상태를 저장하고 복원하려면 UINavigationController의 일부인 UIViewController입니다.스토리 보드없는 UIViewController 상태 복원

내보기 컨트롤러는 UIViewControllerRestoration 프로토콜을 확장합니다. 내가 필요한 모든 메소드를 구현하고 있지만 시뮬레이터에서 encodeRestorableStateWithCoder 또는 decodeRestorableStateWithCoder에 대한 호출이 표시되지 않는다고 생각합니다. MyAppDelegate :

여기 내 응용 프로그램 위임의 모습입니다

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder { 
    NSLog(@"shouldSaveApplicationState"); // seeing this in the debug window 
    return YES; 
} 

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder { 
    NSLog(@"shouldRestoreApplicationState"); // seeing this in the debug window 
    return YES; 
} 

- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder { 
    NSLog(@"willEncodeRestorableStateWithCoder"); // seeing this in the debug window 
} 

- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder { 
    NSLog(@"didDecodeRestorableStateWithCoder"); // seeing this in the debug window 
} 

내가 디버그 창에서 이러한 호출을 모두 참조하십시오. 다음은 응용 프로그램 중단에 표시 내 MyMainViewController이, 모습의 같은 :

@interface MyMainViewController : UIViewController <UIViewControllerRestoration> 
@end 

@implementation MyMainViewController 

- (id)init { 
    if (self = [super init]) { 
     self.restorationIdentifier = @"MyViewControllerRestoreId"; 
     self.restorationClass = [self class]; 
    } 
    return self; 
} 

- (void)loadView { 
    self.view = [[UIView alloc] initWithFrame:frame]; 

    // Bunch of work to create my custom views 
} 

- (void)awakeFromNib { 
    self.restorationClass = [self class]; 
} 

// Expected a call to this method, but not seeing it in debug window 
+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents 
                  coder:(NSCoder *)coder { 
    NSLog(@"viewControllerWithRestorationIdentifierPath: %@", identifierComponents); 

    MyMainViewController *vc = [[MyMainViewController alloc] init]; 
    vc.restorationIdentifier = [identifierComponents lastObject]; 
    vc.restorationClass = [MyMainViewController class]; 

    return vc; 
} 

// Expected a call to this method, but not seeing it in debug window  
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder { 
    NSLog(@"encodeRestorableStateWithCoder"); 
    [super encodeRestorableStateWithCoder:coder]; 
} 

// Expected a call to this method, but not seeing it in debug window  
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder { 
    NSLog(@"decodeRestorableStateWithCoder"); 
    [super decodeRestorableStateWithCoder:coder]; 
}  
@end 

내가 보존 및 복원 테스트 시뮬레이터를 사용하는 방법 : 나는 '다른 모르겠어요

1. Launch app in Simulator via Xcode 
2. Invoke the Home key so App is now suspended. This where I see: 
    shouldSaveApplicationState 
    willEncodeRestorableStateWithCoder 
3. End the debugging session in XCode (Command .) 
4. Start a new debug session of the app in XCode. I see: 
    shouldRestoreApplicationState 
    didDecodeRestorableStateWithCoder 

을 이 일을하기 위해 실종되었습니다.

+0

작업 해 보셨습니까? 여기에 붙어있어 : –

+0

@Jonas Gardner : 문제가 무엇인지 찾아 냈습니까?이 문제도 있습니다. 감사합니다. – Loc

답변

1

이것이 내가 상태 보존/복원을 배우는 중일 때 도움이되는지 또는 차이를 만들지는 모르겠지만 여기에 나와있는 튜토리얼에서 얻은 것부터 시도해 볼 수 있습니다.

VC가 TabBar 또는 NavController에있는 경우 해당 VC에도 restoreIdentifier가 있는지 확인하십시오.

+0

사용중인 튜토리얼은 무엇입니까? –

+0

전자 책 iOS 6 자습서, 여기 http : //www.raywenderlich.com/store/ios-6-by-tutorials – vichudson1

+0

그 사이트는 많은 위대한 튜토리얼을 무료로 가지고 있지만 그 책은 책에 있습니다. 나는 iOS 5 & 6 권의 책을 소유하고 있습니다. 사용 가능한 경우 iOS 7 서적을 구입하십시오. 내 의견으로는 돈 가치가 있습니다. – vichudson1

3

나는 똑같은 문제가있었습니다. 당신은 이미 다 가지고있는 것처럼

<UIViewControllerRestoration> 

것 같다,하지만 나를 위해 그 일을 결국 다음 restorationClass가 사용하는 밝혀졌다, 나는 명시 적으로 위해 뷰 컨트롤러의 프로토콜을 설정하는 데 필요한.

+0

UIViewControllerRestoration의 일부 샘플 코드를 게시 할 수 있습니까? 네가 말한 것처럼 노력했지만 작동하지 않았다. 나는 스토리 보드를 사용하지 않는다. 감사! – Loc

1

이 코드가 실행되지 않을 수 있습니다

- (id)init { 
    if (self = [super init]) { 
     self.restorationIdentifier = @"MyViewControllerRestoreId"; 
     self.restorationClass = [self class]; 
    } 
    return self; 
} 

지정 초기화를보기 컨트롤러에 대한 것은 initWithNibName:bundle:, 그래서 그 init 이제까지 호출된다는 보장은 없습니다. 또한보기 컨트롤러가 xib 또는 스토리 보드에서 오는 경우 해당 메소드 중 어느 것도 호출되지 않습니다.

당신은 awakeFromNib 구현해야합니까 :

- (void)awakeFromNib { 
    self.restorationClass = [self class]; 
} 

뷰 컨트롤러가 XIB 또는 스토리 보드에서 오는 경우 실행되지만 그 경우에 당신은 restorationID을 할당 잊어 버린.

관련 문제