gpt4 book ai didi

ios - 使 UIPickerView 结果导入文本到 UILabel

转载 作者:行者123 更新时间:2023-11-29 01:20:30 24 4
gpt4 key购买 nike

我想让用户从 UIPickerView 中选择的内容直接填充到它下面的 UILabel 中。更具体地说,如果他们选择“维生素 A”,它将以 UILabel 中的文本形式提供以下信息。我是 Xcode 和 Swift 的新手。我已经让选取器在 UILabel 中工作并连接,但还没有弄清楚如何在不复制 UIPickerView 中已有的文本的情况下将两者连接起来。这是我到目前为止的代码:

@IBOutlet weak var Description: UILabel!
@IBOutlet weak var picker: UIPickerView!

var pickerData: [String] = [String]()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Connect data:
self.picker.delegate = self
self.picker.dataSource = self

// Input data into the Array:
pickerData = ["Pick a Vitamin", "Vitamin A", "Vitamin B1", "Vitamin B2", "Vitamin B3", "Vitamin B5", "Vitamin B6", "Biotin", "Folic Acid", "Vitamin B12", "PABA", "Choline", "Inositol", "Vitamin C", "Vitamin D", "Vitamin D3", "Vitamin E"]
}

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


// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.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 pickerData[row]
}
// Catpure the picker view selection
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}

}

最佳答案

didSelectRow 方法更改为如下内容:

// Catpure the picker view selection
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
Description.text = pickerData[row];
}

关于ios - 使 UIPickerView 结果导入文本到 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659027/

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