ios 显示带有选定行的 UIPickerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35708300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:37:57  来源:igfitidea点击:

Showing UIPickerView with selected row

iosswiftuipickerview

提问by Thiha Aung

I am trying to figure out how to show the selected row when i open UIPickerViewafter it has been selected last time.

我试图弄清楚如何在UIPickerView上次选择后打开时显示所选行。

When I open my PickerView again, I want my UIPickerViewto set selected row which I choose at last time.

当我再次打开我的 PickerView 时,我希望我UIPickerView设置我上次选择的选定行。

Is there any way in Swift?

斯威夫特有什么办法吗?

采纳答案by Vladimir

UIPickerView api allow you to select row in any component in code:

UIPickerView api 允许您在代码中的任何组件中选择行:

func selectRow(_ row: Int,
   inComponent component: Int,
      animated animated: Bool)

So if you stored selected indices for your picker's components just call this method for each component before you show picker again.

因此,如果您为选择器的组件存储了选定的索引,只需在再次显示选择器之前为每个组件调用此方法。

回答by Ahmed Onawale

You can save the last selected row in NSUserDefaults, retrieve the value in viewDidLoad of your view controller and your picker's selected row to the index of the selected value from an array of values.

您可以将最后选定的行保存在 NSUserDefaults 中,将视图控制器的 viewDidLoad 中的值和选择器的选定行检索到值数组中选定值的索引。

class Pick: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

var picker = UIPickerView()
var selected: String {
    return NSUserDefaults.standardUserDefaults().stringForKey("selected") ?? ""
}
var data = ["One", "Two", "Three"]

override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self
    picker.dataSource = self
    if let row = data.indexOf(selected) {
        picker.selectRow(row, inComponent: 0, animated: false)
    }
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return data.count
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return data[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    NSUserDefaults.standardUserDefaults().setObject(data[row], forKey: "selected")
}

}

回答by yesthisisjoe

If this is for storing some kind of setting, you may want to look into using persistent storage like NSUserDefaults. When the picker is changed, save the NSUserDefaults value. Then in your viewDidLoad method you can set the picker view to the row you saved earlier.

如果这是为了存储某种设置,您可能需要考虑使用像 NSUserDefaults 这样的持久存储。更改选择器时,保存NSUSERDEFAULTS值。然后在您的 viewDidLoad 方法中,您可以将选择器视图设置为您之前保存的行。

For example, use these lines when you detect the picker view pickerView has changed to store the row in the key pickerViewValue. Put this in didSelectRow for pickerView.

例如,当您检测到选择器视图pickerView 已更改以将行存储在键pickerViewValue 中时,请使用这些行。把它放在 didSelectRow 中用于pickerView。

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(row, forKey: "pickerViewRow")

Then when you load the view, use this to set the picker to the saved row:

然后当你加载视图时,使用它来将选择器设置为保存的行:

let defaults = NSUserDefaults.standardUserDefaults()
if let pickerViewRow = defaults.stringForKey("pickerViewRow")
{
    pickerView.selectRow(pickerViewRow, inComponent: 0, animated: true)
}

回答by Rishabh Dugar

For Swift 3

对于 Swift 3

 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if(pickerView.tag==0){
        //set some global variable
    }
}