iOS:保存双重选择器的设置(数据持久性)
<p><p>所以我有一个双选择器,用户将每个轮子设置为他们想要的,然后按下一个按钮,该按钮将显示他们的选择。易于清洁和简单,但我想存储该数据以备后用,以便在他们关闭应用程序并重新打开后它不会消失。现在,我知道如何使用 datePicker,但不是 doublePicker。所以我的问题是如何调整我的代码,将 datePickers 信息保存和检索到 doublePicker 中?</p>
<p>这是我的 datePicker 代码:</p>
<pre><code> NSUserDefaults *defaults = ;
// Pulling the date out of my picker
NSDate *selectedDate = ;
;
</code></pre>
<p>然后再次检索它:</p>
<pre><code> - (void)viewDidLoad
{
;
// Get the date. I'm going to use a little shorthand instead of creating
// a variable for the instance of `NSUserDefaults`.
NSDate *storedDate = [ 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.
;
}
</code></pre>
<p>任何帮助将不胜感激,谢谢 :)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在您的 <code>UIPickerViewDelegate</code> 中:</p>
<pre><code> - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *defaults = ;
];
}
</code></pre>
<p>要取出数据:</p>
<pre><code> for (int i = 0; i < doublePicker.numberOfComponents; i++) {
NSInteger *storedInteger = [ integerForKey:;
;
}
</code></pre>
<p><strong>用于存储多个值:</strong></p>
<p>在您的 <code>UIPickerViewDelegate</code> 中:</p>
<pre><code>- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *defaults = ;
NSArray *oldArray = ];
//Make a mutable version so we can change it
NSMutableArray *newArray = ;
if (!newArray) {
newArray = ;
}
//Add The Latest Row To The end of the array. We wrap it in an NSNumber so it can be in an array
];
//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
;
}
];
}
</code></pre>
<p>要取出数据:</p>
<pre><code> for (int i = 0; i < doublePicker.numberOfComponents; i++) {
NSArray *storedIntegers = [ arrayForKey: ;
//I don't know what values you want, but to get an int out of the array:
//int integer = [(NSNumber *) intValue];
//And to set the row for a component:
//;
}
</code></pre></p>
<p style="font-size: 20px;">关于iOS:保存双重选择器的设置(数据持久性),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18112748/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18112748/
</a>
</p>
页:
[1]