iPhone- How to save the view controller object itself?

丶灬走出姿态 提交于 2020-01-05 02:49:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1768903/iphone-how-to-save-the-view-controller-object-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!