I have 4 UIPickerViews, which I act on changes in each individual picker views by using if statements for each picker view name. For one of the pickers, a timer with h:m:s, I am trying to add labels for the time units using this answer.
It works great with a single picker, but when I add into my main code with the other 3 pickers, I only want to return a value for the alarmPicker. I need a return of type UIView outside the if statement, but I'm struggling on what it would be if I'm only creating UILabels for the alarmPicker. If I return UIView() the other pickers do not populate their titleForRow strings.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
// code
}
if pickerView == picker2 {
// code
}
if pickerView == picker3 {
// code
}
if pickerView == alarmPicker {
if let label = pickerView.view(forRow: row, forComponent: component) as? UILabel {
if component == 0, row > 1 {
label.text = String(row) + " hours"
}
else if component == 0 {
label.text = String(row) + " hour"
}
else if component == 1 {
label.text = String(row) + " min"
}
else if component == 2 {
label.text = String(row) + " sec"
}
}
}
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
if pickerView == alarmPicker {
let label = UILabel()
label.text = String(row)
label.textAlignment = .center
return label
}
return UIView()
}