根据之前选择的内容使用选择器视图

Posted

技术标签:

【中文标题】根据之前选择的内容使用选择器视图【英文标题】:Using a picker view based on what has been selected before 【发布时间】:2017-08-11 15:06:34 【问题描述】:

我在这里寻找答案,但不相信这个问题已经得到解答。我有多个使用几个不同选择器的文本框。我希望用户选择材料的第一个文本框,然后根据选择的材料,他们只能看到与其对应的材料类型。在我的代码下面,当我命名我的数组时,你可以看到它说 typeBrassData 或 typeCopperData 所以如果用户在 Brass show 选项上方选择 Brass 或 Copper,有人知道该怎么做吗?下面是我在只有 2 个选择器视图时使用的代码。提前谢谢大家!

import UIKit

class MetalCalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate 

@IBOutlet weak var materialField: UITextField!

@IBOutlet weak var typeField: UITextField!

@IBOutlet weak var volumeField: UITextField!

@IBOutlet weak var quantityField: UITextField!

var pickerMaterial = UIPickerView()

var pickerType = UIPickerView()


var materialData = ["Brass","Copper","CuNiSn", "Cupro-Nickel","High Performance Alloys", "Leaded Brass","Nickel Silvers","Phosphor Bronze","Tin Brass"]

var typeData = ["C101", "C102","C1092","C110","C122","C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C210","C220", "C226", "C230", "C240", "C260","C268","C272", "C350", "C353", "C422", "C425", "C4252", "C510","C511", "C5118", "C519","C521", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "C706", "C710", "C715", "C7250", "C752", "C757","C764","C770", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]

var typeBrassData = ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"]
var typeHighPerformanceData = ["C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]
var typeCopperData = ["C101", "C102","C1092","C110","C122"]
var typeCuproNickelData = ["C706", "C710", "C715"]
var typeLeadedBrassData = ["C350", "C353"]
var typeTinBrassData = ["C422", "C425", "C4252"]
var typePhosphorBronzeData = ["C510","C511", "C5118", "C519","C521"]
var typeCunNiSnData = ["C725"]
var typeNickelSilverData = ["C752", "C757","C764","C770"]

override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.


override func viewDidLoad() 
    super.viewDidLoad()

    pickerMaterial.delegate = self
    pickerMaterial.dataSource = self
    pickerMaterial.tag = 1

    materialField.inputView = pickerMaterial

    pickerType.delegate = self
    pickerType.dataSource = self
    pickerType.tag = 2

    typeField.inputView = pickerType

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MetalCalculatorViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)



    // Do any additional setup after loading the view.



func dismissKeyboard() 
    view.endEditing(true)


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


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



    if pickerView == pickerMaterial 
        return materialData.count

     else if pickerView == pickerType
        return typeData.count
    

    return 1


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


    if pickerView == pickerMaterial 
        return materialData[row]

     else if pickerView == pickerType
        return typeData[row]
    
    return ""




func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if pickerView == pickerMaterial 
        materialField.text = materialData[row]
     else if pickerView == pickerType
        typeField.text = typeData[row]
    

【问题讨论】:

【参考方案1】:

你可以使用dictionary数据结构来设置pickerviews的值

var materialSelected = ""

var typeArr = [String]()


//I have done for only three materials obviously you can extend it...

var dict = ["Brass" : ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"],
            "Copper" : ["C101", "C102","C1092","C110","C122"], "CuNiSn" : ["C725"]] as [String: Any]

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


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



  if pickerView == pickerMaterial 
        return materialData.count

     else if pickerView == pickerType
        typeArr = dict[materialSelected] as! [String]
        return typeArr.count
    

    return 1


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


    if pickerView == pickerMaterial 
        return materialData[row]

     else if pickerView == pickerType
        typeArr = dict[materialSelected] as! [String]
        return typeArr[row]
    
    return ""




func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if pickerView == pickerMaterial 
        materialSelected = materialData[row]
        materialField.text = materialSelected

     else if pickerView == pickerType

        typeArr = dict[materialSelected] as! [String]
        typeField.text = typeArr[row]

    

【讨论】:

以上是关于根据之前选择的内容使用选择器视图的主要内容,如果未能解决你的问题,请参考以下文章

根据在选取器视图中选择的行组件更新表视图数据

在添加另一个选择器视图之前,我如何检查超级视图中是不是还有其他选择器视图?

根据其他选择器视图中的选择查看选择器更新

如何根据 android studio 中的微调器选择更改 imageview 视图?

根据行内容显示或隐藏 DGrid 选择器

Android如何根据选择器意图中的用户操作请求权限