首页 > 解决方案 > Having trouble integrating multiple pickerviews in the same view

问题描述

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.

标签: iosswiftpickerview

解决方案


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.


推荐阅读