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
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 ?