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

时间:2017-08-11 15:06:35

标签: ios arrays swift picker

我在这里寻找答案,但不相信这个问题已得到解答。我有多个文本框,使用几个不同的选择器。我希望用户选择材料的第一个文本框,然后根据所选材料,他们只能看到与其对应的材料类型。下面在我的代码中,当我命名我的数组时,你可以看到它说的是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]
    }
}

0 个答案:

没有答案