gpt4 book ai didi

ios - 使用基于之前选择的内容的选择器 View

转载 作者:行者123 更新时间:2023-11-30 12:14:06 24 4
gpt4 key购买 nike

我在这里寻找答案,但不相信这个问题已经得到解答。我有多个文本框,它们使用几个不同的选择器。我希望用户在第一个文本框中选择 Material ,然后根据选择的 Material ,他们只能看到与其对应的 Material 类型。在下面的代码中,当我命名数组时,您可以看到它显示 typeBrassData 或 typeCopperData,因此,如果用户在仅显示 Brass 选项的上方选择 Brass 或 Copper,有人知道如何执行此操作吗?下面是我在只有 2 个选择器 View 时使用的代码。提前感谢大家!

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]
}
}

最佳答案

您可以使用字典数据结构来设置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]

}
}

关于ios - 使用基于之前选择的内容的选择器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45638373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com