Search code examples
iosswiftpickeruipicker

Hide UIPicker after Selection


I have a UIPicker and I want to save selection and hide it after selection. I can't find a way to do this. Can I do this without a "Save" button? Can I just save the selection pressing on the row? Currently I press a button to make the picker appear ( picker.isHidden = false), but then it stays after selection has been made.

  //Picker

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return fiatArray.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return fiatArray[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    displayConvertedValues(forSelected: row)

}

func displayConvertedValues(forSelected row :Int) {

    let chosenfiat = fiatArray[row]
    getData(fiat: chosenfiat)
    tableView.reloadData()
}

Solution

  • If you want to submit you choice, try to implement a mechanism for that, like a toolbar on top of the picker view (like facebook see the screen below). With that mechanism you can save your choice and implement the picker hidden method when you click on the "save button"

    Facebook Picker Screen

    Hope it helps