Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2008, 03:44 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default Save data in text fields

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.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-16-2008, 04:04 PM   #2 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 394
Default

NSUserDefaults Class Reference
__________________
Super Pig
iOwn - Inventory anything and everything.
detz is offline   Reply With Quote
Old 12-16-2008, 05:01 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default Thanks

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?

Thanks for your help.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-16-2008, 05:09 PM   #4 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 394
Default

// 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.
detz is offline   Reply With Quote
Old 12-16-2008, 05:15 PM   #5 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

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.

Enjoy.
RickMaddy is offline   Reply With Quote
Old 12-16-2008, 06:03 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default Thanks

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?
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-16-2008, 06:33 PM   #7 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

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.
RickMaddy is offline   Reply With Quote
Old 12-16-2008, 07:05 PM   #8 (permalink)
Registered Member
 
tkilmer's Avatar
 
Join Date: May 2008
Posts: 583
Default

Look at some of these books. They really helped me get off the ground with objective-C.

1) Programming in Objective-C
2) Cocoa Programming for Mac OS X
3) The C Programming Language

These will all give you a good foundation. None of them are meant directly for iPhone development, but they will give you a good background.
tkilmer is offline   Reply With Quote
Old 12-16-2008, 07:23 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Smile

Thanks guys you've been really helpful
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Reply

Bookmarks

Tags
data, save, textfield

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 259
20 members and 239 guests
ADY, Alsahir, beleg_1998, Dani77, e2applets, iph_s, JamesCahall, JasonR, mer10, Monstertaco, piesia, prchn4christ, Promo Dispenser, Robiwan, Rudy, smithdale87, timle8n1, Touchmint, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,756
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:22 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0