如何保存和恢复状态在iOS中使用故事板?

时间:2016-12-20 12:37:32

标签: ios storyboard state-restoration

我正在开发聊天应用程序。我想存储所有视图控制器的应用程序状态。

我存储应用状态的代码:

+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
 {
 MoreController *vc = nil;
 UIStoryboard *storyboard = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
 if (storyboard)
 {
      vc = (MoreController *)[storyboard instantiateViewControllerWithIdentifier:@"MoreController"];
      vc.restorationIdentifier = [identifierComponents lastObject];
      vc.restorationClass = [MoreController class];
 }
 return vc;
}

 - (void)encodeRestorableStateWithCoder:(NSCoder *)coder
 {
       [coder encodeObject:self.view forKey:@"save"];
       [coder encodeObject:_btnoiwii forKey:@"save"];

      [coder encodeObject:_tblMore forKey:kUnsavedEditStateKey];
           [super encodeRestorableStateWithCoder:coder];
 }

 - (void)decodeRestorableStateWithCoder:(NSCoder *)coder
  {
   self.view = [coder decodeObjectForKey:@"save"];
  _btnoiwii= [coder decodeObjectForKey:@"save"];

  _tblMore = [coder decodeObjectForKey:kUnsavedEditStateKey];
  [super decodeRestorableStateWithCoder:coder];

  }

我能够编码应用程序的状态但不能解码。在app delegate类中,我添加了shouldRestoreApplicationState,willEncodeRestorableStateWithCoder

请给我适当的解决方案来保存和恢复iOS中的应用程序状态。

1 个答案:

答案 0 :(得分:0)

您不应该保存整个表视图并查看控制器的超级视图。当UIKit恢复视图控制器时,它会自动为您恢复所有视图。确保保存数据源并在解码时重新加载表格视图。

如果要恢复tableView的索引,则需要符合此协议 UIDataSourceModelAssociation 并实现以下方法: indexPathForElementWithModelIdentifier modelIdentifierForElementAtIndexPath

还要确保您正在恢复推送视图控制器的导航堆栈。