创建一个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];
要点:
NSUserDefaults非常好用,并不需要用户在程序中设置NSUserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,那么就在哪里创建一个NSUserDefaults对象,然后进行读或者写操作。
针对同一个关键字对应的对象或者数据,可以对它进行重写,重写之后关键字就对应新的对象或者数据,旧的对象或者数据会被自动清理。
以下是一个我自己写的例子,通过checkBtn按钮进行保存用户名和密码:
图片一:保存数据之前;或者上一次没有保存数据,第二次启动的界面
图片二:保存了数据,第二次打开所看到的启动初始界面
- (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];
}