保存UIPicker中选择的值的最佳方法是什么?

时间:2022-02-17 23:08:11

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!

我的视图中有3个UIPickers,我想在我的应用关闭时保存所选的值。我目前正在使用NSUserDefaults来完成其他任务。但我不确定保存UIPickers所选值的语法。谢谢我的同事们!

1 个解决方案

#1


1  

Here is how you can get selected value from UIPickerView -

以下是如何从UIPickerView获取选定值 -

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

and here is how you can save values in NSUserDefaults -

以下是如何在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检索值 -

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"];

#1


1  

Here is how you can get selected value from UIPickerView -

以下是如何从UIPickerView获取选定值 -

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

and here is how you can save values in NSUserDefaults -

以下是如何在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检索值 -

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"];