Search code examples
swiftuipickerview

Set a default value for UIPickerView in Swift?


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.


Solution

  • 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.