NSUserDefaults的使用
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
也可以添加基本数据类型bool,float,int 等,有相应的方法(如下):
setBool:forKey: [accountDefaults setBool:YES forKey:UserDefaultBoolKey];
setFloat:forKey: [[NSUserDefaultsstandardUserDefaults]setFloat:[sendervalue]forKey:@"PlayerVolume"];
setDouble:forKey:
setInteger:forKey:
等等 。
objectForKey: [accountDefaults objectForKey:NCUserDefaultNameKey];
boolForKey: [accountDefaults boolForKey: UserDefaultBoolKey];
以下是一个例子,通过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];
}