开发者

iphone global variables accessed from different views

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 c开发者_开发技巧an't figure this out.


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.


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


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.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜