Reputation: 1
I want to make the user's selection from the UIPickerView
populate a UILabel
directly beneath it. To be more specific, if they select 'Vitamin A' it would then provide information below in the form of text in UILabel
. I am new to Xcode and Swift. I have got the picker working and connected in the UILabel but haven't worked out how to then connect the two up without it just replicating the text already in the UIPickerView. Here's the code I have so far:
@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.
}
}
Upvotes: 0
Views: 477
Reputation: 10172
Change didSelectRow
method to something like this:
// 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];
}
Upvotes: 1