創建一個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];
}