iPhone-如何保存视图控制器对象本身?
我想保存视图控制器的状态。我搜索了一下,发现应该通过保存状态变量来保存状态。
但我对保存视图控制器对象本身感兴趣。有什么方法可以保存视图控制器对象本身,以便当我重新启动应用程序时,我也可以看到与关闭应用程序之前相同的视图状态。否则我必须自己达到这种状态,这将无法完全解决目的,因为(显示的 admob 广告)之类的东西无法恢复。
如果我在某些地方错了或者错过了什么,请指导我。
I want to save the state of my view controller. I searched and found that I should save the state by saving state variables.
But I am interested in saving the view controller object itself. Is there any way to save the view controller object itself so that when I restart the application I see the same view state too on which I was before closing the app. Otherwise I have to reach that state myself and that will not solve the purpose fully as somethings like (the admob ad which was displayed) could not be restored.
If I am wrong at some place or miss something please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑在您想要保留的所有类中实现 NSCoding。如果您正确地实现了这一点,然后在应用程序终止和启动时使用 NSKeyedArchiver/NSKeyedUnarchiver 之类的东西在根节点(例如选项卡栏控制器或导航栏控制器)对对象图进行编码,您可以保存和恢复对象的状态轻松查看控制器。 UIViewController和UIView默认实现它,所以你需要做的就是重写encodeWithCoder:和decodeWithCoder:方法,调用super,并保存你需要的状态变量。
Look into implementing NSCoding in all the classes you want to persist. If you properly implement that and then encode your object graph at a root node (like a tab bar controller or a nav bar controller) with something like NSKeyedArchiver/NSKeyedUnarchiver when your app terminates and starts up, you can save and restore the state of a view controller easily. UIViewController and UIView implement it by default, so all you need to do is override the encodeWithCoder: and decodeWithCoder: methods, call super, and save the state variables you need.