Search code examples
swiftuibuttonuipickerview

Picker View, how to return index of the selected item. Swift 4


I tried to return the index of the selected item in picker View, but I did not succeed. I need to transfer the index of the element when I click the button, then, depending on the index, call a specific function.

@IBAction func myButton(_ sender: UIButton) {
    let row = [pickerView.selectedRow(inComponent: 0)]
}

I tried to print the value of row into the console and it turned out, but when I try to create a condition if row = 0 and etc. I get an error. Help me please!


Solution

  • var row: Int = 0
    var repeatPickerData = [Any]()
    var repeatPickerView: UIPickerView?
    if let aComponent = repeatPickerView?.selectedRow(inComponent: 0) {
        row = aComponent
    }
    strPrintRepeat = repeatPickerData[row]