问题
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