PickerView

class inherits: UIPickerViewDelegate, UIPickerViewDataSource

create a string array as a class variable

in ViewDidLoad:
        self.nameOfPickerView.delegate = self
        self.nameOfPickerView.dataSource = self

implement the following functions

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
 
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return theStringArray.count
    }
 
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return theStringArray[row]
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //what happens when a row gets selected
    }

How to select a row based on a criteria:
Put this in viewDidAppear, not viewDidLoad
var defaultRowIndex = theArray.index(of: theValue)
        if defaultRowIndex == nil
        {
            defaultRowIndex = 0
        }
        self.thePicker.selectRow(defaultRowIndex!, inComponent: 0, animated: false)

Comments

Popular posts from this blog

Setting up a playground

Go to another page