码迷,mamicode.com
首页 > 其他好文 > 详细

UI2_NSUserDefaults

时间:2015-07-11 18:08:29      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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

 

UI2_NSUserDefaults

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4638879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!