菜鸟教程小白 发表于 2022-12-11 18:22:16

ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?


                                            <p><p>我正在开发聊天应用程序。我想存储所有 ViewController 的应用程序状态。</p>

<p>我存储应用状态的代码:</p>

<pre><code>+ (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
{
MoreController *vc = nil;
UIStoryboard *storyboard = ;
if (storyboard)
{
      vc = (MoreController *);
      vc.restorationIdentifier = ;
      vc.restorationClass = ;
}
return vc;
}

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
       ;
       ;

      ;
         ;
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
   self.view = ;
_btnoiwii= ;

_tblMore = ;
;

}
</code></pre>

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

<p>请给我适当的解决方案来保存和恢复 iOS 中的应用程序状态。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你不应该这样保存整个表格 View 和 ViewController 的 superView 。当 UIKit 恢复你的 ViewController 时,它会自动为你恢复所有的 View 。确保保存数据源并在解码时重新加载表格 View 。 </p>

<p>如果你想恢复你的tableView的索引,你需要遵守这个协议(protocol),<strong>UIDataSourceModelAssociation</strong>并实现这些方法:<strong>indexPathForElementWithModelIdentifier</strong>,<strong>modelIdentifierForElementAtIndexPath</strong ></p>

<p>还要确保您正在恢复推送 ViewController 的导航堆栈。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41242549/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41242549/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 中使用 Storyboard保存和恢复状态?