In this tutorial i will be showing you how to save and load data in your applications
Features:
- 1 Label
- 1 Textfield
- 3 Buttons
Saving and loading data in apps is a major component for games such as saving high scores or loading progress or just generally enabling the user to save and load the data they created within you app
The Code
Play the video to get a step by step walkthrough and all code can be copy and pasted |
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { __weak IBOutlet UITextField *textfield; __weak IBOutlet UILabel *label; } -(IBAction)save; -(IBAction)load; -(IBAction)settext; @end
ViewController.m
-(IBAction)save { NSString *savedstring = label.text; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:savedstring forKey:@"savedstring"]; [defaults synchronize]; } -(IBAction)load { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *loadstring = [defaults objectForKey:@"savedstring"]; [label setText:loadstring]; } -(IBAction)settext { label.text = textfield.text; }
视频: http://www.youtube.com/watch?feature=player_embedded&v=I0dP-sNbv94