0

Below is my code, this allows one Textfield to have a pop up pickerView as an input source. I want to know what i need to add to allow a second TextField to also have a pop up pickerView as a input source.

I have managed to get the firstNametextfield working, but havent managed to get the secondNames working.

class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{

@IBOutlet weak var firstNameTextField: UITextField!

@IBOutlet weak var secondNameTextField: UITextField!


var firstNames = ["Lewis", "Jason","Alex","Mason"]

var secondNames = ["Davies", "Jones","Rees","Kristensen"]


var pickerView = UIPickerView()

override func viewDidLoad()

 {

super.viewDidLoad()

pickerView.delegate = self

pickerView.dataSource = self

        firstNameTextField.inputView = pickerView
        firstNameTextField.textAlignment = .center
        firstNameTextField.placeholder = "Select Name"
}

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {

            return firstNames.count

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {

        return firstNames[row]

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
            firstNameTextField.text = reports[row]
    }
Duncan C
  • 128,072
  • 22
  • 173
  • 272

1 Answers1

0
class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{

    @IBOutlet weak var firstNameTextField: UITextField!

    @IBOutlet weak var secondNameTextField: UITextField!


    var firstNames = ["Lewis", "Jason","Alex","Mason"]

    var secondNames = ["Davies", "Jones","Rees","Kristensen"]


    let firstNamePicker = UIPickerView()
    let secondNamePicker = UIPickerView()

    override func viewDidLoad()

    {

        super.viewDidLoad()

        firstNamePicker.delegate = self
        firstNamePicker.dataSource = self
        secondNamePicker.delegate = self
        secondNamePicker.dataSource = self

        firstNameTextField.inputView = firstNamePicker
        firstNameTextField.textAlignment = .center
        firstNameTextField.placeholder = "Select First Name"

        secondNameTextField.inputView = secondNamePicker
        secondNameTextField.textAlignment = .center
        secondNameTextField.placeholder = "Select Second Name"

    }

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

    public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {

        return pickerView == firstNamePicker ? firstNames.count : secondNames.count

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if pickerView == firstNamePicker {
            firstNameTextField.text = firstNames[row]
        } else {
            secondNameTextField.text = secondNames[row]
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return pickerView == firstNamePicker ? firstNames[row] : secondNames[row]
    }
}
u84six
  • 4,604
  • 6
  • 38
  • 65
  • Thanks for the Response But the Line: "return pickerView == firstNamePicker ? firstNamePicker.count : secondNamePicker.count" Doesn't seem to work as it says that UIPickerView has no member count. – Lewis Jones Apr 06 '18 at 11:20
  • @LewisJones sorry, that was a typo on my end. See my edit. – u84six Apr 06 '18 at 14:28
  • Thanks for the help that has made it work, but with one other small issue, within the pop up pickerView each value has been set to "?". But once one of the ? have been selected, it outputs one of the values within the array meaning it works. Just wondered if you knew how/what the issue could be. @u84six Thanks again – Lewis Jones Apr 06 '18 at 18:30
  • @LewisJones To fix your problem, you need to add the picker delegate method titleForRow. I've updated the code with the new method. Can you select my answer as the answer to your question? – u84six Apr 06 '18 at 20:42
  • Thanks thats fixed it and yeah i have selected it as the answer. – Lewis Jones Apr 07 '18 at 11:47