alerts-and-pickers icon indicating copy to clipboard operation
alerts-and-pickers copied to clipboard

default selection on pickerview not working ?

Open subhash08 opened this issue 5 years ago • 1 comments

I show the picker view and press done. The call back of done button not called untill I did not scroll the pickerview.

subhash08 avatar Mar 15 '19 12:03 subhash08

i have figured out how to make that work. @subhash08 What you have to do is to declare the variables before the alert.addPickerView() method. for example, my picker view contains 2 columns: weight and weight unit (kg and lbs).

Okay so what is this action doing? first I set the preselected values. In case the user does not move the picker those values are going to be maintained. Otherwise, the picker.selectedrow method is going to update the value of weight and the same for the weightUnit.

var weight  = Float()
                    var weightUnit = ""
                    var arrayWeight = [String]()
                    let weightValues = stride(from: 0.0, through: 60.0, by: 0.05)
                    for i in weightValues{
                        arrayWeight.append(String(format: "%.2f", i))
                    }
                    let pickerViewSelectedValue: PickerViewViewController.Index = (column: 0, row: Int(3/0.05))
           
    
                    weight = Float(3.00)
                    weightUnit = " kg"
                   
                    alert.addPickerView(values: [arrayWeight, ["kg","lbs"]], initialSelection: pickerViewSelectedValue) { vc, picker, index, values in
                        DispatchQueue.main.async {
                            UIView.animate(withDuration: 1) {
                                
                                weight = Float(picker.selectedRow(inComponent: 0))*Float(0.05)
                                
                                

                            }
                        }
                        print(weight)
                        
                    
                        if picker.selectedRow(inComponent: 1) == 0{
                            weightUnit = " kg"
                        }
                        else if picker.selectedRow(inComponent: 1) == 1{
                            weightUnit = " lbs"
                        }
                        
                        
                    }
```

luchi17 avatar May 14 '19 16:05 luchi17