简体   繁体   English

如何在选择器视图中每次选择/设置选定行作为默认选定行

[英]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.

相关问题 根据在选择器视图中选择的行组件更新表视图数据 - Update table view data based on row component selected in picker view 如何根据选择器视图中选择的行组件更新表视图数据 - How to update table view data based on row component selected in picker view 如何在iPhone的选择器选定行中显示文本字段值? - How to display the textfield value in picker selected row in iPhone? 在表中选择行时,如何通知详细视图选择了哪一行? - When a row is selected in a table, how can I inform the detail view about which row was selected? 如何从所选行设置详细视图的标题? - How do I set a detail view's title from the selected row? 带视图的UIPicker +点击视图的!=选定的行 - UIPicker With View + View Tapped != Selected Row 如何在详细视图中显示与表中所选行相对应的标题 - How to display the title in detail view corresponding to the row selected in the table 如何查看不同的细节在iOS Master Detail上查看选定行 - how to view different detailView of selected row on iOS Master Detail JQuery Mobile:如何对split视图实施did select方法,以在详细信息视图中显示相应的选定行的信息和详细信息 - JQuery Mobile: How to implement did select method to the Split view, to display corresponding selected row's information and details in details view 选择器视图的代码是如何选择的? - how is the code for picker view selected item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM