不使用故事板的应用程序状态保存和恢复?

问题描述:

我在添加uinavigationcontroller AppDelegate.m这样不使用故事板的应用程序状态保存和恢复?

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
nav.restorationIdentifier = @"nav1"; 
self.window.rootViewController = nav; 

ViewController.m这个样子的

- (void)viewDidLoad 
{ 
     [super viewDidLoad]; 
     self.restorationIdentifier = @"secondViewController"; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(IBAction)click:(id)sender { 

    secondViewController *svc = [[secondViewController alloc]initWithNibName:@"secondViewController" bundle:nil]; 
     svc.restorationIdentifier = @"secondViewController"; 

     svc.restorationClass = [self class]; 
    [self.navigationController pushViewController:svc animated:YES]; 
} 

secondViewController.m

+(UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder 
{ 
    NSLog(@"viewControllerWithRestorationIdentifierPath"); 
    UIViewController * myViewController = 
    [[secondViewController alloc] 
    initWithNibName:@"secondViewController" 
    bundle:nil]; 

    return myViewController; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 

    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    label.text = @"sdfkheiowodhnskjfdgewluri3y2oejdscndjshfiledhdsfhewilufhyeows"; 

    // Do any additional setup after loading the view from its nib. 
} 

-(void)encodeRestorableStateWithCoder:(NSCoder *)coder 
{ 
    NSLog(@"encodeRestorableStateWithCoder"); 

    [coder encodeObject:label.text forKey:@"UnsavedText"]; 

    [super encodeRestorableStateWithCoder:coder]; 
} 

-(void)decodeRestorableStateWithCoder:(NSCoder *)coder 
{ 
    NSLog(@"decodeRestorableStateWithCoder"); 

    [super decodeRestorableStateWithCoder:coder]; 

    label.text = [coder decodeObjectForKey:@"UnsavedText"]; 
} 

在输出

我得到encodeRestorableStateWithCoder但毕竟这当我按下home键,然后再次运行应用程序我的应用程序的状态不存在secondViewController和decodeRestorableStateWithCoder不会被调用。

我不知道我在做什么错?

+0

现在我通过添加self.restorationIdentifier = @“secondViewController”来获取输出窗口中的所有日志; self.restorationClass = [self class];但导航控制器总是显示第一个视图...请任何帮助。 –

+0

我有同样的问题,我有故事板中的所有控制器,你有任何解决方法..请帮助 看到这个问题我已经发布 http://stackoverflow.com/questions/25197980/issue-with-state -preservation和 - 恢复功能于IOS?noredirect = 1#comment39241814_25197980 – Sabby

我得到了它在appdelegate.m

NSString * const kRootViewControllerKey = @"RootViewKey"; 

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self commonInitWithOptions:launchOptions]; 
    return YES; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self commonInitWithOptions:launchOptions]; 
    return YES; 
} 

- (void)commonInitWithOptions:(NSDictionary *)launchOptions { 

    static dispatch_once_t predicate; 
    dispatch_once(&predicate,^{ 

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     // Override point for customization after application launch. 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

     UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
     nav.restorationIdentifier = @"NavigationController"; 
     self.window.rootViewController = nav; 
     [self.window makeKeyAndVisible]; 
    }); 
} 

// Encode app delegate level state restoration data 
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder { 
    [coder encodeObject:self.window.rootViewController forKey:kRootViewControllerKey]; 
} 

// Decode app delegate level state restoration data 
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder { 

    // Find the preserved root view controller and restore with it 
    UINavigationController *navControlller = [coder decodeObjectForKey:kRootViewControllerKey]; 

    if (navControlller) { 
     self.window.rootViewController = navControlller; 
    } 

} 

无论- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder也不- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder实现增加这方面的工作。