Load String automatically on launch
Hey 开发者_StackOverflow中文版everyone, I have a little issue that I can't seem to figure out. What Im trying to do is have pre-saved values automatically put into the textfields on launch. Right now, I have the user filling out the fields, then pressing a save button. At next launch, the user needs to press the load button, how do i eliminate that load button?
In your Save button action, do this ...
[[NSUserDefaults standardUserDefaults] setObject:myKey1TextField.text forKey:@"key1"];
... and in your viewWillAppear
method do this ...
myKey1TextField.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"key1"];
... viewWillAppear
is not the only place where you can place it. It depends on how frequently these data are changed, if they can be changed in background, etc.
Look at the NSUserDefaults API.
Basically you save your content with key/pair, so when your app is launched you just retrieve these values.
I did something like this the other day but i am using core data. I just stored the previous value my core data file and read it back the next time the app launches, lot of mucking around if you don't have core data already but just putting it out there for anyone with a similar problem
精彩评论