Hey everyone, I'm trying to save text in my text fields when the app is closed. I have two text fields containing the number of right and wrong answers a user has made. I just have a variable incrementing everytime they get an answer right and an answer wrong and I then set the .text property of the text field to the value of this variable.
How do I get the text field to keep these values even when the app is closed so they remain when the app is launched again.
Thanks Detz, I find the documentation pretty confusing though. Any chance you could show me a quick example of how to implement it to save the value of a texfield?
// To save the text
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[textField text] forKey:@"textfield1"];
[defaults synchronize];
//When you restore
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"textfield1"]){
[textField setText:[[NSUserDefaults standardUserDefaults] stringForKey:@"textfield1"]];
}
Something like this...
__________________ Super Pig iOwn - Inventory anything and everything.
If you can't read the docs then, to be blunt, you're hosed. It's critical that you learn how to help your self by searching and understanding the documentation. I spend at least half my time scouring the docs while I write code. At least when trying to do something new. It's amazing how much can be learned by just reading what classes there are and what methods they have. Plus there are all the programming guides that show how to do so many things.
This is meant to be a helpful message, not just a lame slam.
Now for your immediate question, break the problem up into steps.
1) Get current text from text field
2) Persist value when app terminates
3) Read persisted value when app restarts
4) Updated text field with restored value
I'm guessing you know how to do 1 and 4 so you need help with 2 and 3.
NSUserDefaults is the proper way to do this. If you look at the docs for this class it lists 5 sample apps that use this class. This is another useful skill. Reading other code and extracting what you need from them.
Now having given this long discussion - what is it about the docs that confuse you? Maybe we can help you better understand them.
Thanks for your help both of you, think I have got it working now.
RickMaddy, I realise you are just trying to help and not slamming me. I jsut find the documentation confusing. For example if I search for CGRect and am shown:
struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
Fields
origin
A CGPoint structure that specifies the coordinates of the rectangle’s origin. The origin is located in the lower-left of the rectangle.
size
A CGSize structure that specifies the height and width of the rectangle.
What am I supposed to do with that? Do I just replace 'origin' and 'size' with the origin and size I want and put the code in my method?
This is telling you that CGRect is a C structure (struct) containing two fields - origin and size of type CGPoint and CGSize respectively. As it turns out each of these are also C structures (which you can tell by clicking on CGPoint or CGSize.
So yes, if you create a CGPoint object then you can just assign it to the 'origin' field of a CGRect object. Or you can just assign one of the CGPoint fields (x and y).
Code:
CGRect myRect;
CGPoint myPoint;
myPoint.x = 34.5;
myPoint.y = 12.0;
myRect.origin = myPoint;
// or you can do:
myRect.origin.x = 34.5;
myRect.origin.y = 12.0;
C Structures aren't classes. You don't allocate them.
Objective-C is a superset of C. If you aren't familiar with C then I suggest you pickup a decent book on learning C. Understanding the basics of C will make you a better Objective-C programmer.