UI2_NSUserDefaults

//

//  ViewController.m

//  UI2_NSUserDefaults

//

//  Created by zhangxueming on 15/7/8.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.



    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *textString = [defaults objectForKey:@"key"];

    if (textString) {

        NSLog(@"textString = %@",textString);

    }

    else

    {

        NSLog(@"程序第一次启动");

    }



    UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    writeBtn.frame = CGRectMake(100, 100, self.view.frame.size.width-200, 50);

    [writeBtn setTitle:@"写入数据" forState:UIControlStateNormal];

    [writeBtn addTarget:self action:@selector(writeBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:writeBtn];

    

    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    deleteBtn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50);

    [deleteBtn setTitle:@"删除数据" forState:UIControlStateNormal];

    [deleteBtn addTarget:self action:@selector(deleteBtnClicked) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:deleteBtn];

}



- (void)writeBtnClicked

{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:@"千锋教育" forKey:@"key"];

    [defaults synchronize];

}





- (void)deleteBtnClicked

{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults removeObjectForKey:@"key"];

    [defaults synchronize];

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 

你可能感兴趣的:(NSUserDefaults)