OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 中使用 Storyboard保存和恢复状态? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:22
标题: ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?

我正在开发聊天应用程序。我想存储所有 View Controller 的应用程序状态。

我存储应用状态的代码:

+ (UIViewController *)viewControllerWithRestorationIdentifierPathNSArray *)identifierComponents coderNSCoder *)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)encodeRestorableStateWithCoderNSCoder *)coder
 {
       [coder encodeObject:self.view forKey"save"];
       [coder encodeObject:_btnoiwii forKey"save"];

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

 - (void)decodeRestorableStateWithCoderNSCoder *)coder
  {
   self.view = [coder decodeObjectForKey"save"];
  _btnoiwii= [coder decodeObjectForKey"save"];

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

  }

我可以对应用程序的状态进行编码,但不能解码。在应用委托(delegate)类中,我添加了 shouldRestoreApplicationState,willEncodeRestorableStateWithCoder

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



Best Answer-推荐答案


你不应该这样保存整个表格 View 和 View Controller 的 super View 。当 UIKit 恢复你的 View Controller 时,它会自动为你恢复所有的 View 。确保保存数据源并在解码时重新加载表格 View 。

如果你想恢复你的tableView的索引,你需要遵守这个协议(protocol),UIDataSourceModelAssociation并实现这些方法:indexPathForElementWithModelIdentifiermodelIdentifierForElementAtIndexPath

还要确保您正在恢复推送 View Controller 的导航堆栈。

关于ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41242549/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4