0

I am trying to set up two pickerviews on one view, but I don't want to show the wheels, I just want the wheels to pop up when the specific text box is clicked. I can get it to work for one Pickerview, but when I try and add the second, the pickerviews no longer work.

I've tried to use "tags" but since I'm not expicitly using UIPickerView!, I can't use the tags, I don't think.

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    @IBOutlet weak var cornerText: UITextField!
    @IBOutlet weak var brandText: UITextField!


    //    PICKER VIEW CODE

    let Corner = ["LF", "RF", "RR", "LR", "5th"]
    let Brand = ["Brand1", "Brand2", "Brand3", "Brand4", "Brand5"]


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

    func pickerView(_ cornerPickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if cornerText.isEditing{
            return Corner.count
        }
        else if brandText.isEditing{
            return Brand.count
        }
        else {
            return 0
        }
    }

    func pickerView(_ cornerPickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if cornerText.isEditing{
            return Corner[row]
        }
        else if brandText.isEditing{
            return Brand[row]
        }
        else {
            return nil
        }
    }
    var cornerIndex: Int!
    var brandIndex: Int!

    func pickerView(_ cornerPickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if cornerText.isEditing{
            cornerText.text = Corner[row]
            cornerIndex = row
        }
        else if brandText.isEditing{
            brandText.text = Brand[row]
            brandIndex = row
        }
        else{
            return
        }
    }

    func createPickerView(){


        if cornerText.isEditing{
            let cornerPickerView = UIPickerView()
            cornerPickerView.delegate = self
            cornerText.inputView = cornerPickerView
        }
        else if brandText.isEditing{
            let cornerPickerView = UIPickerView()
            cornerPickerView.delegate = self
            brandText.inputView = cornerPickerView
        }
        else {
            return
        }
    }

    func dismissPickerView(){
        let toolBar = UIToolbar()
        toolBar.sizeToFit()

        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
        toolBar.setItems([doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        if cornerText.isSelected{
            cornerText.inputAccessoryView = toolBar
        }
        else {
            brandText.inputAccessoryView = toolBar
        }
    }

    @objc func dismissKeyboard(){
        view.endEditing(true)
        //   Put anything that must pop up based on the corner here

I would like both pickerviews to work, right now neither pickerview's display.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
bb4
  • 15
  • 3
  • *"the pickerviews no longer work"* - this is not enough information. Please [edit] your question (don't reply in a comment) with specific details about exactly in what way it isn't working. Provide errors, actual versus expected behavior, etc. – rmaddy Aug 15 '19 at 23:47
  • why are you using two pickerViews? you can simply use one pickerView for both the textfields. just reload pickerView components to call it's delegate when textfield is being first responder. – Jayraj Vala Aug 16 '19 at 06:33

1 Answers1

0

Your picker view data source/delegate code looks OK. The problem appears to be with how you create and setup the input view and input accessory view for each text field.

You only need one picker view and one tab bar. Assign each to both text fields.

Remove your dismissPickerView method and update your createPickerView method as follows:

func createPickerView() {
    let pickerView = UIPickerView()
    pickerView.delegate = self
    pickerView.dataSource = self

    cornerText.inputView = pickerView
    brandText.inputView = pickerView

    let toolBar = UIToolbar()
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
    toolBar.setItems([doneButton], animated: false)

    cornerText.inputAccessoryView = toolBar
    brandText.inputAccessoryView = toolBar
}

Since both text fields share the same picker view, you will need to reload the picker view each time a text field begins editing. Implement the textFieldDidBeginEditing delegate method, access the picker view and reload it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you so much! That made the Pickerviews work as expected. The only thing missing in your code is the toolBar.sizeToFit() line of code to make the "Done" button appear. – bb4 Aug 20 '19 at 23:52