gpt4 book ai didi

swift - 如何在对 SegmentedControl 执行操作后在 PickerView 中设置行

转载 作者:行者123 更新时间:2023-11-28 14:31:29 24 4
gpt4 key购买 nike

我是 Swift 和 Xcode 的新手,我想将行设置为关系中的 PickerView,其中选择了 SegmentedControl 中的 Segment。我尝试了以下方法将数组 firstInputUnitChooseData 设置为我的 PickerView firstInputUnitChoose 的行:

class FirstViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var wantedInputProperty: UISegmentedControl!
@IBOutlet weak var firstInputUnitChoose: UIPickerView!

var wantedInputPropertyData: [String] = [String]()
var firstInputUnitChooseData: [String] = [String]()

override func viewDidLoad() {
super.viewDidLoad()

// Connect data
self.firstInputUnitChoose.delegate = self
self.firstInputUnitChoose.dataSource = self

wantedInputProperty.addTarget(self, action: #selector(FirstViewController.doSomething), for: .valueChanged)

}

@IBAction func doSomething() {
let choosableFirstUnit = wantedInputProperty.titleForSegment(at: wantedInputProperty.selectedSegmentIndex)
switch choosableFirstUnit {
case "p", "p,h", "p,s", "p,t", "p,x", "p,\u{03C1}":
firstInputUnitChooseData = ["Bar", "MPa", "Pa"]
case "t", "t,x" :
firstInputUnitChooseData = ["°C", "K"]
case "h,s", "h,\u{03C1}":
firstInputUnitChooseData = ["J/Kg", "kJ/Kg"]
default:
firstInputUnitChooseData = ["X"]
}
}

// The number of columns of data
func numberOfComponents(in pickerView: UIPickerView) -> Int {

return 1
}

// Number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

return firstInputUnitChooseData.count
}

// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

return firstInputUnitChooseData[row]
}

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

// Create action for selected PickerView row
}

如果我在 func DoSomething() 中使用 print(chooseableFirstUnit)print(firstInputUnitChooseData) 它会将正确的数据打印到控制台。

例如:如果我设置数组

firstInputUnitChooseData = ["Bar", "MPa", "Pa"]

直接在 func viewDidLoad() 中,它可以工作,但这不是我想要的。

最佳答案

你需要重新加载 pickerView ,所以在函数结束时 doSomething do

self.firstInputUnitChoose.reloadAllComponents()

关于swift - 如何在对 SegmentedControl 执行操作后在 PickerView 中设置行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51218074/

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