2

私はチャットアプリケーションに取り組んでいます。すべてのView Controllerのアプリケーション状態を保存したい。

アプリの状態を保存する私のコード:

+ (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];

  }

アプリケーションの状態をエンコードできますが、デコードできません。アプリデリゲートクラスで追加しました shouldRestoreApplicationState,willEncodeRestorableStateWithCoder

iOSでアプリケーションの状態を保存および復元するための適切な解決策を教えてください。

4

1 に答える 1

0

テーブルビュー全体とView Controllerのスーパービューをそのように保存しないでください。UIKit がビュー コントローラーを復元すると、すべてのビューが自動的に復元されます。必ずデータ ソースを保存し、デコード時にテーブル ビューを再読み込みしてください。

tableView のインデックスを復元する場合は、このプロトコルUIDataSourceModelAssociationに準拠し、次のメソッドを実装する必要があります: indexPathForElementWithModelIdentifiermodelIdentifierForElementAtIndexPath

また、ビュー コントローラーがプッシュされているナビゲーション スタックを回復していることを確認してください。

于 2017-01-06T18:10:23.910 に答える