[英]How to select/set a selected row in picker view as default selected row every time
I have a picker view 我有一个选择器视图
[picker selectRow:3 inComponent:0 animated:YES];
when i wrote this code in view did load, 当我在视图中编写此代码时,
This method is used to set the row 3 as default row in picker. 此方法用于将第3行设置为选择器中的默认行。
But i want that the row i have selected is set to be as default for the next time. 但我希望将我选择的行设置为下一次的默认行。
what to do? 该怎么办? thanks in advance...
提前致谢...
Why dont you save the last pressed row in a variable and you load it next time? 为什么不将最后按下的行保存在变量中,然后下次加载呢?
Something like: 就像是:
int myLastPressed;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
myLastPressed = row;
}
You can use a variable in your code, or a global one in NSUSerDefaults 您可以在代码中使用变量,也可以在NSUSerDefaults中使用全局变量
And then next time: 然后下一次:
[picker selectRow:myLastPressed inComponent:0 animated:YES];
Try like this 试试这样吧
NSUserDefaults *usrDefaults = [NSUserDefaults standardUserDefaults];
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[usrDefaults setInteger:row forKey:@"Index"];
}
[pickerView selectRow:[[NSUserDefaults standardUserDefaults]integerForKey:@"Index"] inComponent:0 animated:NO];
This question is very unprecise. 这个问题非常不准确。 What do you want to do?
你想让我做什么?
selectRow:inComponent:
is exactly what you have to call, if you want a specific row selected. 如果要选择特定的行,
selectRow:inComponent:
正是您必须调用的。 If you want to save the selected state for the next appstart, you could save the current selected row (read with selectedRowInComponent
) in the UserDefaults: [[NSUserdefault standardUserDefaults] saveInteger: selectedRow forKey @"savedSelectedPickerRowComponent1"]
如果要保存下一次启动的选定状态,可以将当前选定的行(用
selectedRowInComponent
读取)保存在UserDefaults中: [[NSUserdefault standardUserDefaults] saveInteger: selectedRow forKey @"savedSelectedPickerRowComponent1"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.