I have a view with three picker views in it. Two of the picker views have the same data, an array with the numbers 1 to 100. The third picker view has an array with a list of model railroad track manufacturers in it. I have tagged the picker views using a method I found on this site, but when I run the app, all three picker views have 1 to 100 as their data. I also control-dragged from all picker views to the yellow circle at the top of the view and clicked dataSource and delegate. How do I use multiple picker views with different data sources in one view? Also, in order to make the code run, I had to delete weak from all @IBOutlet statements relating to the picker views. Is this a bad thing to do? I am relatively new to code. Thanks.
Picker View Scene Screen Shot
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
//MARK: Properties
@IBOutlet var layoutLengthPickerView: UIPickerView!
@IBOutlet var layoutWidthPickerView: UIPickerView!
@IBOutlet var trackPickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
layoutLengthPickerView = UIPickerView()
layoutWidthPickerView = UIPickerView()
trackPickerView = UIPickerView()
layoutLengthPickerView.tag = 0
layoutWidthPickerView.tag = 1
trackPickerView.tag = 2
}
let numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
let manufacturers = ["Atlas True Track", "Atlas Code 100", "Atlas Code 83", "Bachmann Nickel Silver", "Bachmann Steel Alloy", "Kato", "Life-Like Trains Code 100", "LIfe-Like Trains Power-Loc", "Peco Code 100", "Peco Code 83", "Peco Code 75", "Shinohara Code 100", "Shinohara Code 70", "Walthers"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
return numbers[row]
} else if pickerView.tag == 1 {
return numbers[row]
} else if pickerView.tag == 2 {
return manufacturers[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return numbers.count
} else if pickerView.tag == 1 {
return numbers.count
} else if pickerView.tag == 2 {
return manufacturers.count
}
return 1
}
}
See Question&Answers more detail:
os