创建一个user defaults方法有多个,最简单得快速创建方法:
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
添加数据到 user defaults:
[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
也可以添加基本数据类型int, float, bool等,有相应得方法
[accountDefaults setBool:YES forKey:UserDefaultBoolKey];
从user defaults中获取数据:
[accountDefaults objectForKey:NCUserDefaultNameKey]
[accountDefaults boolForKey: UserDefaultBoolKey];
今天用到NSUserDefault的时候,一直以为[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"testInfo"] ;就算存储好了,再[[NSUserDefaults standardUserDefaults] arrayForKey:@"testInfo"]就算取出来,可是发现重启动程序,关键字testInfo对应的竟然是空,仔细查下资料,发现自己太大意了,忘了写到disk里面了![[NSUserDefaults standardUserDefaults] synchronize]; 涨学问了又。。。呵呵!!!!
下面贴出代码:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"111" forKey:@"myTest"];
[defaults synchronize];
NSString *testStr = [defaults objectForKey:@"myTest"];
NSLog(@"testStr is: %@",testStr);
注意问题:
通过查看官方文档。UserDefaults不是立即写入,而是根据时间戳定时的把缓存中的数据写入本地磁盘。所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了。出现以上问题。可以通过调用synchornize方法强制写入。
要点:
1)NSUserDefaults非常好用,并不需要用户在程序中设置NSUserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,那么就在哪里创建一个NSUserDefaults对象,然后进行读或者写操作。
2)针对同一个关键字对应的对象或者数据,可以对它进行重写,重写之后关键字就对应新的对象或者数据,旧的对象或者数据会被自动清理。
- (void)defaultCheck:(id)sender
{
NSLog(@" check按钮被按下 ");
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
if ([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == NO)
{
((UIButton*)sender).selected = YES;
[accountDefaults setBool:YES forKey:NCUserDefaultBoolForRegister];
[accountDefaults setObject:nameField.text forKey:NCUserDefaultNameKey];
[accountDefaults setObject:pwdField.text forKey:NCUserDefaultPasswordKey];
}
else {//YES
((UIButton*)sender).selected = NO;
[accountDefaults setBool:NO forKey:NCUserDefaultBoolForRegister];
[accountDefaults setObject:nil forKey:NCUserDefaultNameKey];
[accountDefaults setObject:nil forKey:NCUserDefaultPasswordKey];
}
}
- (void)loadView {
[super loadView];
nameField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
pwdField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
[nameField setCenter:CGPointMake(160, 100)];
[pwdField setCenter:CGPointMake(160, 150)];
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
//存的是bool值一定要用boolForKey:获取
//如果之前已经保存了User Defaults,那么在读取的时候就要将数据读取出来,显示在用户名和密码框中(这里没有加密)。
if ([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == YES) {
nameField.text = [accountDefaults objectForKey:NCUserDefaultNameKey];
pwdField.text = [accountDefaults objectForKey:NCUserDefaultPasswordKey];
}
else
{
[nameField setPlaceholder:@"用户名"];
[pwdField setPlaceholder:@"密码"];
}
nameField.borderStyle = UITextBorderStyleBezel;
[pwdField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:nameField];
[self.view addSubview:pwdField];
UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginBtn setFrame:CGRectMake(0, 0, 50, 30)];
[loginBtn setCenter:CGPointMake(200, 200)];
[loginBtn addTarget:self action:@selector(loginToWeiBo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];
UIButton * checkBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[checkBtn setFrame:CGRectMake(0, 0, 20, 20)];
[checkBtn setCenter:CGPointMake(160, 180)];
//选中之后得图标
[checkBtn setBackgroundImage:[UIImage imageNamed:@"lock.png"] forState:(UIControlState)UIControlStateHighlighted];
[checkBtn setBackgroundImage:[UIImage imageNamed:@"lock.png"] forState:(UIControlState)UIControlStateSelected];
//未选中
[checkBtn setBackgroundImage:[UIImage imageNamed:@"unlock.png"] forState:(UIControlState)UIControlStateNormal];
[checkBtn addTarget:self action:@selector(defaultCheck:) forControlEvents:UIControlEventTouchUpInside];
//用户名和密码已经保存
if([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == YES)
{
[checkBtn setSelected:YES];
}
[self.view addSubview:Btn];
}