views:

562

answers:

3

Okay, so ultimately I want a program where the user can input simple data such as their name, date of birth, address, etc. and then have that information stay through multiple views. I am having the user input their information as UITextFields but their are multiple views that they are using to input the data. Is there a way that when the user inputs data in a UITextField - then moves to another view - then returns to the original view - that the data will still be in that UITextField? I figure since there are placeholders that there must be a command to show previously written text in that field when the viewController is called.

Also, for the life of me, I can't figure out how to keep these variables global. I have read in multiple areas that I should define them in the AppDelegate as a simple:

NSString *userName;
NSString *userDOB;

But how do I assign the strings from the UITextFields in a different view to these variables and then re-assign them to the UITextFields when the user returns to the place where they originally input them?

(I apologize if I am not explaining this coherently - I am a bit of a newb)

-EDIT- I have followed the link below but still can't figure this out. I tried using:

NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];

[[NSUserDefaults standardUserDefaults]setObject:@"Horace" forKey:@"name"];

but I must not be using them in the correct place. Where would i use this commands? If I am having the user input the variables in say "ViewController1" - should I put these commands somewhere in the "ViewController1.m" file? For the life of me I can't figure this out.

A: 

think you have two questions here:

  1. How do I preserve data in a view so that when I return to it, it's still there?
  2. How do I store data so that all my views can share it?

The first one I think I can answer easily. If you have one controller per view which is the recommended practice. Then typically you would have properties on that view which you link you UITextFields to via IBOutlet. That data will not be cleared until you manually do so or the view is wiped from memory. So you don't have to worry about gong to another view and coming back. You data is preserved.

The second question is harder because there are a number of ways. I don't have much experience in this issue myself, but here's what I see are the alternatives:

  • You can use globals as you talk about.
  • You can also use Core Data if the data is complex enough to warrant it.
  • You can create properties of the application delegate. There is a static method on UIApplication which you can call from anywhere to get a reference to the delegate.
Derek Clarkson
If I dealloc the variables at the bottom of each viewController, does that mean that they are immediately dealloced when I move to another view? If they ARE actually stored as I move through the views, how do I make them them show up in the UITextFields that the user has already typed in? For example, if they are prompted for their Name and type "Jim" and then move to another view, when they return, that UITextField is usually blank as it was when they were prompted - how do I have "Jim" already placed in that field when they return to that view?
Rob
dealloc only occurs when the view is cleared from memory. Therefore the properties of the view will be preserved. If your textfield is being cleared, then either you are causing the view to be unloaded from memory, or you have some code somewhere else that is clearing the field.Sorry, but I don't really have the experience to be able to point exactly at what is wrong. I'm still learning this myself :-)
Derek Clarkson
Okay, that makes sense - but how do I know when the view is being cleared from memory? We don't dealloc viewcontrollers or anything so how do I know when the view is being cleared? I've noticed that if I stop deallocating the variables at the bottom of the view - it will keep the information when I move forward to a new view and then return - but not if I move backward and then forward again. Does that make sense?
Rob
A: 

You may refer to a earlier question. [NSUserDefaults standardUserDefaults] is an option.

ohho
+1  A: 

Either the App Delegate or NSUserDefaults are good options for global values, depending on what you are doing with them and hopw frequently they will be accessed. You can overload this method to get what you want:

-(void)viewDidAppear:(BOOL)animated

For example:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    MyProjectAppDelegate* appDelegate = (MyProjectAppDelegate*)[[UIApplication sharedApplication] delegate];
    userNameTextField.text = appDelegate.userName;
    userDOBTextField.text = appDelegate.userDOB;
}

When this view appears, it will load the values from the App Delegate.

A second option would be to keep a reference to the view controller, probably again in the App Delegate. Then, whenever you want to move between views, just add and remove the view as needed. Don't alloc/init it every time, only the first.

alku83
thank you this was actually what I was looking for
Elijah