39

How do I set the starting row of the picker view in Swift?

I see there is a similar question for Objective-C, but I don't understand the code.

pkamb
  • 33,281
  • 23
  • 160
  • 191
KML
  • 2,302
  • 5
  • 26
  • 47

5 Answers5

107

This is the code I have used in one of my apps:

// Declare the outlet to the picker in your storyboard
@IBOutlet var myPicker: UIPickerView!

//...

override func viewDidLoad() {
//...
    myPicker.selectRow(row, inComponent: 0, animated: true)
}

Obviously, replace row and 0 with whatever values you want.

starball
  • 20,030
  • 7
  • 43
  • 238
mtaylor
  • 1,130
  • 1
  • 8
  • 6
  • Hi, I tried your suggestion but it gave me an error. Maybe because I have already called it?? in : class ViewController: UIViewController, UIPickerViewDelegate { And maybe that conflicts? I tried also to put it further down together with the func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{ return 3 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ switch component { etc... – KML Sep 18 '14 at 22:48
  • Could you give me more details about the error you received? Did you remember to connect the outlet to a UIPickerView in your storyboard? – mtaylor Sep 19 '14 at 14:24
  • Hey, Thanks for following up, I was busy changing the pickerview font in another color and I had to change quite bit of the whole code to implement some code suggestions from @POB. I just finished it and implemented your code in the new ViewController now. It worked like a charm !! I am not sure what was wrong with the old code. THANK YOU SO MUCH – KML Sep 19 '14 at 16:19
  • 1
    Question: If I set the row as above and want the picker to open on that specific row, then how do I do this in viewDidLoad() after I select the row? – Nate Uni Jan 07 '15 at 02:21
  • Instance variables do not begin with a capital letter. – Gusutafu Nov 17 '16 at 12:35
  • Just a tip - make sure you call this before constraints, as for me I got conflicting constraints for some reason if I put it after. – George Jan 15 '19 at 20:04
14
@IBOutlet weak var mPicker: UIPickerView!
var items: [String] = ["NoName1","NoName2","NoName3"] // Set through another ViewController
var itemAtDefaultPosition: String?  //Set through another ViewController

//..
//..

var defaultRowIndex = find(items,itemAtDefaultPosition!)
if(defaultRowIndex == nil) { defaultRowIndex = 0 }
mPicker.selectRow(defaultRowIndex!, inComponent: 0, animated: false)
5

SWIFT 5

The function "SetDefaultValue" will position the pickerView on the desired string by finding the item position in the pickerData which is used to populate the picker view. In this example it will be "item 2"

var pickerView = UIPickerView()
var pickerData = ["item 1","item 2","item 3","item 4","item 5"]
var defaultItem = "item 2"

func setDefaultValue(item: String, inComponent: Int){
 if let indexPosition = pickerData.firstIndex(of: item){
   pickerView.selectRow(indexPosition, inComponent: inComponent, animated: true)   
 }
}
STerrier
  • 3,755
  • 1
  • 16
  • 41
0

Default value selected in pickerview in swift 3

If Your array like bellow

let pickerArray  = [
        ["displayValue":"--Select--" as AnyObject,"id":"" as AnyObject],
        ["displayValue":"Jame" as AnyObject,"id":"100" as AnyObject],
        ["displayValue":"Enamul" as AnyObject,"id":"201" as AnyObject]
    ]

Just add bellow line for default selected in pickerview

 pickerViewTextField.text  = pickerArray[0]["displayValue"] as! String
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

Try to put this line myPicker.selectRow(row, inComponent: 0, animated: true) in override func viewDidAppear

Mutasm
  • 29
  • 3
  • In the accepted answer the same solution is mentioned, except it's placed in `viewDidLoad()`. Can you explain why you think it's better to do it in `viewDidAppear()`? – Eric Aya Jun 08 '21 at 14:07
  • in my code when I put it in viewDidLoad() I did not take any result it seemed like the app cycle bypassed it but when wrote it it in viewDidAppear() it worked well – Mutasm Jun 14 '21 at 09:31
  • viewDidLoad() is only called when the view is created, whereas viewWillAppear() is called every time the view will appear. – bias Jul 12 '22 at 04:04