简体   繁体   English

显示带有选定行的UIPickerView

[英]Showing UIPickerView with selected row

I am trying to figure out how to show the selected row when i open UIPickerView after it has been selected last time. 我试图弄清楚在上次选择UIPickerView后如何显示所选行。

When I open my PickerView again, I want my UIPickerView to set selected row which I choose at last time. 当我再次打开PickerView时,我希望UIPickerView设置我最后一次选择的选定行。

Is there any way in Swift? Swift有什么办法吗?

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")
}

}

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. 因此,如果存储选择器组件的选定索引,则在再次显示选择器之前,请为每个组件调用此方法。

If this is for storing some kind of setting, you may want to look into using persistent storage like NSUserDefaults. 如果这是用于存储某种设置,则可能要考虑使用NSUserDefaults之类的持久性存储。 When the picker is changed, save the NSUserDefaults value. 更改选择器后,保存NSUserDefaults值。 Then in your viewDidLoad method you can set the picker view to the row you saved earlier. 然后,在viewDidLoad方法中,可以将选择器视图设置为之前保存的行。

For example, use these lines when you detect the picker view pickerView has changed to store the row in the key pickerViewValue. 例如,当您检测到选择器视图pickerView已更改为将行存储在键pickerViewValue中时,请使用这些行。 Put this in didSelectRow for pickerView. 将此放入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)
}

For Swift 3 对于Swift 3

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM