gpt4 book ai didi

ios - Swift:UIpickerView 在 UIlabel 上返回数字

转载 作者:行者123 更新时间:2023-11-28 15:20:19 26 4
gpt4 key购买 nike

我的 UIpickerView 中有 3 个组件,我想返回 UIpickerView 上显示的所有 3 个数字,但它只返回最后一个被选中的数字。换句话说,UIlabel 只显示一位数字。这是我目前所拥有的

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var label: UILabel!
@IBOutlet weak var pickerView: UIPickerView!

let numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return numbers[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return numbers.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
label.text = numbers[row]
}

最佳答案

请检查:

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

let val1 = numbers[pickerView.selectedRow(inComponent: 0)]
let val2 = numbers[pickerView.selectedRow(inComponent: 1)]
let val3 = numbers[pickerView.selectedRow(inComponent: 2)]

label.text = "\(val1) \(val2) \(val3)"
}

关于ios - Swift:UIpickerView 在 UIlabel 上返回数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46126096/

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