问题
So I have a double picker where the user sets each wheel to what they want, and then press a button which will display their choice. Easy clean and simple, but I want to store that data for later so that it does not disappear after they close the app and re-open it again. Now, I know how to do it with a datePicker, but not a doublePicker. So my question is how do I tweak my code from saving and retrieving a datePickers info into a doublePicker?
Here is my code for the datePicker:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Pulling the date out of my picker
NSDate *selectedDate = [self.datePicker date];
[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];
And then retrieving it again:
- (void)viewDidLoad
{
[super viewDidLoad];
// Get the date. I'm going to use a little shorthand instead of creating
// a variable for the instance of `NSUserDefaults`.
NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];
// Setting the date on the date picker. I'm passing `NO` to `animated:`
// because I'm performing this before the view is on screen, but after
// it has been loaded.
[self.datePicker setDate:storedDate animated:NO];
}
Any help would be appreciated, thx :)
回答1:
In your UIPickerViewDelegate
:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:[NSString stringWithFormat:@"DoublePickerViewController.%i", component]];
}
To get the data out:
for (int i = 0; i < doublePicker.numberOfComponents; i++) {
NSInteger *storedInteger = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:@"DoublePickerViewController.%i", i];
[doublePicker selectRow:storedInteger inComponent:i animated:NO];
}
For storing multiple values:
In your UIPickerViewDelegate
:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *oldArray = [defaults arrayForKey:[NSString stringWithFormat:@"DoublePickerViewController.%i", component]];
//Make a mutable version so we can change it
NSMutableArray *newArray = [oldArray mutableCopy];
if (!newArray) {
newArray = [NSMutableArray array];
}
//Add The Latest Row To The end of the array. We wrap it in an NSNumber so it can be in an array
[newArray addObject:[NSNumber numberWithInteger:row]];
//If you want to have a limit on the amount of values that can be stored then use this
int maxValues = 5;
if (newArray.count >= maxValues) {
//Remove the oldest object
[newArray removeObjectAtIndex:0];
}
[defaults setObject:newArray forKey:[NSString stringWithFormat:@"DoublePickerViewController.%i", component]];
}
To get the data out:
for (int i = 0; i < doublePicker.numberOfComponents; i++) {
NSArray *storedIntegers = [[NSUserDefaults standardUserDefaults] arrayForKey: [NSString stringWithFormat:@"DoublePickerViewController.%i", i];
//I don't know what values you want, but to get an int out of the array:
//int integer = [(NSNumber *)[storedIntegers objectAtIndex:0] intValue];
//And to set the row for a component:
//[doublePicker selectRow:storedInteger inComponent:i animated:NO];
}
来源:https://stackoverflow.com/questions/18112748/ios-saving-a-double-pickers-settings-data-persistence