I have a 2 UIPickerView's, one called dayPicker where you have an option between 1-12 and durationPicker where you have the options of days, weeks, months and years.
The code for this is below:
class AddNewViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var dayPicker: UIPickerView!
@IBOutlet weak var durationPicker: UIPickerView!
var durationDay: Double?
var durationType: String?
var durationPickerDataSource = ["Day(s)","Week(s)","Month(s)","Year(s)"];
var dayPickerDataSource = ["1","2","3","4","5","6","7","8","9","10","11","12"];
func numberOfComponentsInPickerView(dayPickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == durationPicker {
return durationPickerDataSource.count;
}
else {
return dayPickerDataSource.count;
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == durationPicker{
durationType = durationPickerDataSource[row]
print("")
print("")
print("\(row): type == \(durationType)")
print("\(row)")
print("")
print("")
return durationType
}
else {
durationDay = Double(dayPickerDataSource[row])
return dayPickerDataSource[row]
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.durationPicker.dataSource = self;
self.durationPicker.delegate = self;
self.dayPicker.dataSource = self;
self.dayPicker.delegate = self;
}
}
when I select the rows in the simulator it prints all of the rows correctly apart from the last one where it prints "Week(s)".
I have the following constraints on the pickers:
dayPicker:
durationPicker:
Copied your code, everything seems to work fine. My only guess is that you are modifying your data sources and therefore the picker delegate returns the wrong information.
I tested your code (without modifications), the pickers display the correct values...
From the comment section it seems the problem lies in not having specified the UIPickerView
's didSelectRow
method, adding it seemingly solved the OP's problem (can't elaborate on the "why" though...):
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == self.dayPicker{
print("picked \(dayPickerDataSource[row])")
}else {
print("picked \(durationPickerDataSource[row])")
}
}