tags:

views:

120

answers:

2

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

A: 

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.

KennyTM
and if i leave from my 5 th view ,then how to come back to 1st view
if i go to 5 th view after processing 1,2,3,4then,how to come back to 5 th view wen i'm coming from another application to my application
You can store the identifier of the view as an integer by `-setInteger:forKey:`, and then restore it by `-integerForKey:` and do anything you want with this integer. It is very flexible. But I don't understand what you mean by "come back to 5th view". Is that kind of 5th descendant *a la* `UINavigationController` or 5th element in a form *a la* `UITextField`?
KennyTM
You have to push all 5 view controllers on the nav controller's stack. Make sure you have `animated:NO` when you do that.
lucius
I'm saying that i leave from 5 th view means that i go to 5 th view ,then press home button and then go to another application then again press home button and then back to my application at that time always 1 st view opens but as i leaved from 5 th view then 5 th view must be openedwhat should i do for that?
You still haven't answer whether it's `UINavigationController`-like (which they're called *view controllers* not views) or `UITextField`-like. If it's the former you need to save the whole path to that *view controller* too.
KennyTM
what is this forKey:@"firstTextField"] here?
A: 

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.

Kevlar