Pickerview is showing empty on second click in the textfield, adn when i press Done or Cancel the App crashes

143 views Asked by At
var gender = ["MALE","FEMALE", "Other"]
var genderPicker: UIPickerView! = UIPickerView()

// On the first click in the textfield the gender pickerview works fine, but if i press out and then i press again in the gender textfield the picker appears empty.

// And when i press either cancel or Done to dismiss the picker the app crashes with the below error

Unrecognized selector sent to instanceā€ in swift

This screenhot shows the second time i press in the textfield

override func viewDidLoad() {
    super.viewDidLoad()
    tableViewHeight.constant = screenHeight * 0.7
    // Do any additional setup after loading the view.
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor.systemBlue
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
    
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    
    genderTxtField.delegate = self
    genderPicker!.delegate = self
    genderPicker!.dataSource = self
    genderTxtField.inputAccessoryView = toolBar
    self.genderTxtField.inputView = genderPicker
    
}

func donePicker() {
    genderTxtField.resignFirstResponder()
}

// Delegates Methods

extension InsuranceMethodsSemiViewController: UITextFieldDelegate {
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        genderPicker.isHidden = false
        genderTxtField.inputView = genderPicker;
        return false
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        genderPicker.isHidden = true
    }
}

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    genderTxtField.text = gender[row]
    self.view.endEditing(true)
    pickerView.isHidden = true
}
}

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

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

There are 1 answers

3
Shehata Gamal On BEST ANSWER

1- Add _

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    genderPicker.isHidden = false
    genderTxtField.inputView = genderPicker;
    return true
}

2- Change action to #selector(donePicker)

let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))

With

@objc func donePicker() {