Best way to save values selected in UIPicker?

365 views Asked by At

I have 3 UIPickers in my view, I would like to save the selected values for when my app closes. Im currently using NSUserDefaults for everything else. But I wasn't sure of the syntax for save selected values of UIPickers. Thanks my fellow coders!

1

There are 1 answers

0
Saurabh On

Here is how you can get selected value from UIPickerView -

[yourPickerDataArray objectAtIndex:[pickerView selectedRowInComponent:0]];

and here is how you can save values in NSUserDefaults -

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

and here is how you can retrieve values from NSUserDefaults -

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];