我正在使用以下代码从设置视图中保存值,该设置视图从 UITextField 获取值并将它们与 NSUserDefaults 一起存储。下面的代码甚至调用了同步但它没有保存更改。我在这里做错了什么?
- (IBAction)save {
NSLog(@"save");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (self.usernameTextField.text != nil) {
NSLog(@"username: %@", self.usernameTextField.text);
[defaults setObject:kTwitterUsernameKey forKey:self.usernameTextField.text];
}
if (self.passwordTextField.text != nil) {
NSLog(@"password: %@", self.passwordTextField.text);
[defaults setObject:kTwitterPasswordKey forKey:self.passwordTextField.text];
}
[defaults synchronize];
[self dismissModalViewControllerAnimated:TRUE];
}