我想让用户从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.
}
}
答案 0 :(得分:1)
将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];
}