开发者

how to maintain state for so many views of particular application

开发者 https://www.devze.com 2022-12-16 03:32 出处:网络
I have 1application. Ithasalmost 32 views in hiereachywith back button. Iwanttomaintainstate ofeach and every view andalso ifi bcak the state of that view also maintains

I have 1 application. It has almost 32 views in hiereachy with back button. I want tomaintain state of each and every view and also if i bcak the state of that view also maintains is there any way to do it by code or by the setting

I'm asking for iphone

My simple question is if i put two text fields in my view and then i press home button and then,i move to another application and then again i press home button and then i move to my application ,i'm able to open that view which i leave and also my textfield or whatever i fill will 开发者_运维知识库be maintained


In your application delegate's -applicationWillTerminate:, save the info by e.g.

....
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
[defs setObject:myFirstTextField.text forKey:@"firstTextField"];
[defs setObject:mySecondTextField.text forKey:@"firstTextField"];
...

And in -applicationDidFinishLaunching: or -application:didFinishLaunchingWithOptions:, restore the info by

....
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
myFirstTextField.text = [defs stringForKey:@"firstTextField"];
mySecondTextField.text = [defs stringForKey:@"firstSecondField"];
...

In general, whatever you want to save and restore can be placed into NSUserDefaults.


If you implement the NSCoding methods on all your views and view controllers, you can save all your stuff in a keyed archiver that can easily be restored at a later time. You should implement

-(void)encodeWithCoder:(NSCoder*)coder

to save all your classes' instance variable data, and

-(id)initWithCoder:(NSCoder*)coder

to restore it later. Look at the NSCoding documentation; there should be an official apple guide on how to implement and use this properly.

The nice thing about all this is that UIView and UIViewController and their subclasses all implement these as well, so for instance if you encode a UINavigationController you will automatically have all of the view controllers that exist on the navigation stack encoded for free as well, which should get you what you need if you implement the NSCoding methods in your own classes as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消