保存和加载数据 Save and load data(实例)

 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值