建立一個user defaults方法有多個,最簡單得快速建立方法:
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
添加資料到 user defaults:
[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
也可以添加基礎資料型別 (Elementary Data Type)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];}
文章出處:http://xiaohui3837843.blog.163.com/blog/static/5438874020115823834836/