UIPickerView can't pre select a row

For the app I'm working on, what I'd like would be that when the view show, the pickerview view is already at a certain row, and that I don't need to touch it (unless I need it).

I know there are plenty of question like this one on SO, but I couldn't find my answer.

Right now here is what's done and what works:

  • When the view appears, the UIPickerView is at the preselected row (ie 2nd for example)
  • I can change it to another one and save it.

But, when I try to save without touching it, it's like nothing is selected. I've to touche the PickerView to select the row I want, even if it's the one I "Selected" at first.

I'm selected the row with :

pickerViewAction.selectRow(inPickerViewAction.index(of: temp?[0] as! String)!, inComponent: 0, animated: true)

Where

inPickerViewAction.index(of: temp?[0] as! String)!

is the row I need.

How I test if a row is selected (maybe my problem comes from here, I don't know...)

let selectedValue1 = inPickerViewAction[Row] 
switch selectedValue1 { 
     case "Email" : 
     // DOING MY STUFF
     case "Commande http", name1, name2, name3, name4, name5: 
     //DOING MY STUFF
     default: let alert = UIAlertController(title: "Nothing selected", message:"You must select an item before saving", preferredStyle: UIAlertControllerStyle.alert)
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) break

Right now if I don't touch the UIPickerView, I've the UIAlert that show.

If anyone have an idea, that'd be great. Thanks

Answered by Claude31 in 255610022

I've tested on a small example and that works perfectly (XCode 8)


I just have in viewDidLoad:


class MyViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet myPicker : UIPickerView!

override func viewDidLoad() {
     super.viewDidLoad()
     myPicker.selectRow(3, inComponent: 0, animated: true)

}

// And the delegate func for pickerView


So, can you show your viewController definition, the problem is probably there.


Can you also tell exactly what you get for selectedValue1.

Yopu say "SelectedValue1 contains the String of the row selected in the tableView I'm coming from."

That's what you expect ! But is it the case ? Show the real values from log, to confirm.


It should match exactly one of the following (including upper/lowercase, space, …:) and obviously it does not:

"Email" :

"Commande http", name1, name2, name3, name4, name5:

What are values of name1 to 5 ?

You give partial information. Error may comme from parts you don't show.


What is the content of selectedValue1 ?

print to log:

let selectedValue1 = inPickerViewAction[Row]
print("selectedValue ", selectedValue1)


When you select the row :

pickerViewAction.selectRow(inPickerViewAction.index(of: temp?[0] as! String)!, inComponent: 0, animated: true)


- What is temp ? Here again, log

print("temp[0] ", temp?[0] as! String)

- where do you call pickerViewAction.selectRow ? Should be in viewDidLoad

Test also the selection immediately after selectRow

print("SelectedRow ", pickerViewAction.selectedRow(inComponent: 0))

Thanks for the reply.


Wasn't showing everything because it's a code with multiple swift files and links between them.


The thing that I don't understand is that the pickerview is at the row that I want, but looks like it's not selected...

Everything works well, but that.


SelectedValue1 contains the String of the row selected in the tableView I'm comming from.

temp = Optional([Row previously selected in pickerview I want to set in the pickerView, data1, data2, data3])

print("SelectedRow ", pickerViewAction.selectedRow(inComponent: 0)) show the row selected in the tableView I'm comming from (Int)

SelectedValue1 = the string of the row selected in the pickerView that I want to save.

Accepted Answer

I've tested on a small example and that works perfectly (XCode 8)


I just have in viewDidLoad:


class MyViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet myPicker : UIPickerView!

override func viewDidLoad() {
     super.viewDidLoad()
     myPicker.selectRow(3, inComponent: 0, animated: true)

}

// And the delegate func for pickerView


So, can you show your viewController definition, the problem is probably there.


Can you also tell exactly what you get for selectedValue1.

Yopu say "SelectedValue1 contains the String of the row selected in the tableView I'm coming from."

That's what you expect ! But is it the case ? Show the real values from log, to confirm.


It should match exactly one of the following (including upper/lowercase, space, …:) and obviously it does not:

"Email" :

"Commande http", name1, name2, name3, name4, name5:

What are values of name1 to 5 ?

Hum, ok, I checked once more this morning, and it appeared I had some trouble with a variable using the same name as another one.
Plus a little other problem.


Now it looks like it's working.


Thanks, you show me where could be my mistake(s).

you need to call the delegate didSelect :

//first
pickerViewAction.selectRow(0, inComponent: 0, animated: true)
//Then
pickerView(pickerViewAction, didSelectRow: 0, inComponent: 0)
UIPickerView can't pre select a row
 
 
Q