无法在一个ViewController上使用两个UIPickerViews

时间:2019-04-26 02:41:45

标签: ios swift uipickerview

我目前有一个包含3个文本字段的视图控制器:

customerTextField

manufacturerTextField

modelTextField

选择ManufacturerTextField时,我需要显示与https://www.example.com/example/Manufacturer.php关联的UIPicker。

根据设计,此UIPicker具有使用JSON数据填充的两列。 选择时,UIPicker会同时填充manufacturerTextFieldmodelTextField

当用户选择customerTextField时,我希望UIPickerView显示来自https://www.example.com/example/customer.php的数据

这只能是单列选择。

更新的问题:

以下是我当前拥有的内容,由于某种原因,当选择customerTextField时,两列的UIPicker附带了makererTextField数据。

    import UIKit

class CreateRMA_ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {



        @IBOutlet weak var customerTextField: UITextField!
        @IBOutlet weak var manufacturerTextField: UITextField!
        @IBOutlet weak var modelTextField: UITextField!

        var selectedTextField = UITextField()

    struct Category {
        var name: String
        var items: [String]
    }
    var allCategories = [Category]()
    var selectedCategory:Category?
    var selectedItem: String?

    var pickerView: UIPickerView!
    var values: [AnyObject] = []

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    selectedTextField = textField
    pickerView.isHidden = false
    return true
}

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        if selectedTextField == customerTextField {
            return 1
        } else if selectedTextField == manufacturerTextField {
            return selectedCategory == nil ? 1 : 2
        } else {//modelTextField
            return 2
        }
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if selectedTextField == customerTextField {
            return values.count
        } else if selectedTextField == manufacturerTextField {
            return component == 0 ? allCategories.count : selectedCategory?.items.count ?? 0
        } else {//modelTextField
            return selectedCategory?.items.count ?? 0
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if selectedTextField == customerTextField {
            return values[row] as? String
        } else if selectedTextField == manufacturerTextField {
            if component == 0 {
                return allCategories[row].name
            } else {
                return selectedCategory?.items[row]
            }
        } else {//modelTextField
            return selectedCategory?.items[row]
        }
    }


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

        if component == 0 {
            selectedCategory = allCategories[row]
            manufacturerTextField.text = allCategories[row].name
            pickerView.reloadAllComponents()
        } else {
            selectedItem = selectedCategory?.items[row]
            modelTextField.text = selectedCategory?.items[row]
        }
    }







    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


        override func viewDidLoad() {
            super.viewDidLoad()

            pickerView = UIPickerView()
            pickerView.dataSource = self
            pickerView.delegate = self
            manufacturerTextField.inputView = pickerView






            //get the values from sql/Json
            let url = NSURL(string: "https://www.example.com/test/service.php")

            let data = NSData(contentsOf: url! as URL)
            var tmpValues = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
            tmpValues = tmpValues.reversed() as NSArray
            reloadInputViews()


            for candidate in tmpValues {
                if let cdict = candidate as? NSDictionary {

                    //fullName is the column name in sql/json
                    let names = cdict["customer"]
                    self.values.append(names! as AnyObject)


                }
            }

            if let url = URL(string: "https://www.example.com/test/Make_Model.php"),
                let data = try? Data(contentsOf: url),
                let tmpValues = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [[String:String]] {
                let tempCategories = tmpValues?.reduce(into: [String:[String]](), { (dict, value) in
                    if let manufacturer = value["manufacturer"], let model = value["model"] {
                        dict[manufacturer, default:[]].append(model)
                    }
                })
                for category in (tempCategories ?? [:]) {
                    allCategories.append(Category(name: category.key, items: category.value))
                }
                pickerView.reloadAllComponents()
            }
    }



}

1 个答案:

答案 0 :(得分:1)

当多个文本字段具有与输入视图相同的选择器视图时,您需要保留对当前所选文本字段的引用

@IBOutlet weak var customerTextField: UITextField!
@IBOutlet weak var manufacturerTextField: UITextField!
@IBOutlet weak var modelTextField: UITextField!
var selectedTextField = UITextField()

将选定的文本字段分配给textFieldShouldBeginEditing中的此临时文本字段

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    selectedTextField = textField
    pickerView.reloadAllComponents()
    pickerView.isHidden = false
    return true
}

然后检查在选择器视图数据源和委托方法中选择了哪个文本字段,并使用适当的数据源。

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    if selectedTextField == customerTextField {
        return 1
    } else if selectedTextField == manufacturerTextField {
        return selectedCategory == nil ? 1 : 2
    } else {//modelTextField
        return 2
    }
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if selectedTextField == customerTextField {
        return values.count
    } else if selectedTextField == manufacturerTextField {
        return component == 0 ? allCategories.count : selectedCategory?.items.count ?? 0
    } else {//modelTextField
        return selectedCategory?.items.count ?? 0
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if selectedTextField == customerTextField {
        return values[row] as? String
    } else if selectedTextField == manufacturerTextField {
        if component == 0 {
            return allCategories[row].name
        } else {
            return selectedCategory?.items[row]
        }
    } else {//modelTextField
        return selectedCategory?.items[row]
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if selectedTextField == customerTextField {
        customerTextField.text = values[row] as? String
    } else if selectedTextField == manufacturerTextField {
        if component == 0 {
            selectedCategory = allCategories[row]
            manufacturerTextField.text = allCategories[row].name
            pickerView.reloadAllComponents()
        } else {
            selectedItem = selectedCategory?.items[row]
            modelTextField.text = selectedCategory?.items[row]
        }
    } else {//modelTextField
        modelTextField.text = selectedCategory?.items[row]
    }
}