如何根据另一个pickervew更改pickerview中的选择?

Posted

技术标签:

【中文标题】如何根据另一个pickervew更改pickerview中的选择?【英文标题】:How can I change the choices in a pickerview based on another pickervew? 【发布时间】:2020-02-14 13:00:51 【问题描述】:

Picture of current version我目前有两个选择器视图。一个是国家选择器视图(美国和加拿大),另一个是州选择器视图。 I'd like to have it so when USA is selected it displays US states, and when Canada is selected, Canadian provinces are displayed.这是我到目前为止所拥有的示例。非常感谢您的帮助!

编辑:仍然没有找到任何可行的解决方案,任何帮助将不胜感激!

let countries = ["",
"USA",
"Canada",
]
let states = ["Alaska",
"Alabama",
"Arkansas"]
 let provinces = ["Alberta",
"British Columbia",
"Manitoba",
"New Brunswick"]

//Sets picker view
let pickerView = UIPickerView()
//Holds current data in array
var currentArray: [String] = []
//Holds current text field
var activeTextField : UITextField!
var activeField: UITextField?

 fileprivate func delegates() 
  countryTextView.delegate = self
  stateTextView.delegate = self


fileprivate func pickerViewDelegates() 
    //Enables pickerview
    pickerView.delegate = self
    pickerView.dataSource = self

//Sets pickerview input
fileprivate func pickerViewSet() 
    countryTextView.inputView = pickerView
    stateTextView.inputView = pickerView

//Closes keyboard when done button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool 
    textField.resignFirstResponder()
    return true

//Textfield delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
    activeTextField = textField

    switch textField 
    case countryTextView:
        currentArray = countries
    case stateTextView:
        currentArray = states
    default:
        print("Default")
    
    pickerView.reloadAllComponents()
    return true

//Number of rows in picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1

//One element of array per row
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return currentArray.count

//Puts the array element of whichever picker view is chosen into picker view
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    return currentArray[row]

//Selects the array element in the row thats chosen
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    print("Selected item is", currentArray[row])
    activeTextField.text = currentArray[row]

【问题讨论】:

有人有可行的解决方案吗? 【参考方案1】:

我并不是说我的代码“不错”。它只是你想要的(据我了解)

如果您从 Blubb 更改为 USA,第二个选择器将会更新。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 


let countries = ["Blubb",
                 "USA",
                 "Canada",
]
let states = ["Alaska",
              "Alabama",
              "Arkansas"]

let statesCan = ["1", "2"]

//Sets picker view
let pickerView = UIPickerView()
let pickerView2 = UIPickerView()
//Holds current data in array
var currentArray: [String] = []
//Holds current text field
var activeTextField : UITextField!
var activeField: UITextField?

fileprivate func delegates() 
//        countryTextView.delegate = self
//        stateTextView.delegate = self


fileprivate func pickerViewDelegates() 
    //Enables pickerview
    pickerView.delegate = self
    pickerView.dataSource = self

    pickerView2.delegate = self
    pickerView2.dataSource = self

//Sets pickerview input
fileprivate func pickerViewSet() 
//        countryTextView.inputView = pickerView
 //        stateTextView.inputView = pickerView

//Closes keyboard when done button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool 
    textField.resignFirstResponder()
    return true


//Number of rows in picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1

//One element of array per row
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    if pickerView == self.pickerView2 
        return currentArray.count
     else 
        return countries.count
    

//Puts the array element of whichever picker view is chosen into picker view
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    if pickerView == self.pickerView2 
        return currentArray[row]
     else 
        return countries[row]
    

//Selects the array element in the row thats chosen
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if pickerView == self.pickerView 
        if row == 0 
            currentArray = states
         else 
            currentArray = statesCan
        
        pickerView2.reloadAllComponents()
    
//     activeTextField.text = currentArray[row]


override func viewDidLoad() 
    super.viewDidLoad()

    currentArray = states

    pickerViewDelegates()
    self.view.addSubview(pickerView)
    self.view.addSubview(pickerView2)

    pickerView2.frame.origin.y = 400

    



【讨论】:

啊,它可以工作,但问题是它创建了一个选择器下拉列表,但我希望它位于文本字段本身的内部。我添加了一张我现在拥有的照片。

以上是关于如何根据另一个pickervew更改pickerview中的选择?的主要内容,如果未能解决你的问题,请参考以下文章

如何根据另一个数据框更改数据框的元素?

检查另一个输入时如何根据一个输入更改值?反应钩子

如何根据同一数据库中的另一个值更改mysql中的值?

如何使用 Angular 6 + Handsontable 根据另一个单元格值仅更改一个单元格值?

如何根据 Django admin 中的另一个选择标签选项更改选择标签选项?

R ggplot 直方图。如何根据另一个变量更改条形的颜色?