iPhone-如何保存视图控制器对象本身? [英] iPhone- How to save the view controller object itself?
问题描述
我要保存我的视图控制器的状态。我搜索并发现我应该保存状态通过保存状态变量。
I want to save the state of my view controller. I searched and found that I should save the state by saving state variables.
但我有兴趣保存视图控制器对象本身。有任何方法来保存视图控制器对象本身,以便当我重新启动应用程序,我看到相同的视图状态,我在关闭应用程序之前。否则,我必须自己达到那个状态,并不能完全解决目的,像(显示的admob广告)无法恢复。
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.
如果我
推荐答案
查看在你想要持久化的所有类中实现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.
这篇关于iPhone-如何保存视图控制器对象本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!