Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default selection on pickerview not working ? #81

Open
subhash08 opened this issue Mar 15, 2019 · 1 comment
Open

default selection on pickerview not working ? #81

subhash08 opened this issue Mar 15, 2019 · 1 comment

Comments

@subhash08
Copy link

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

@luchi17
Copy link

luchi17 commented May 14, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants