1

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()
    }
bruno617
  • 62
  • 6
  • Have you tried returning `view` instead of `UIView`? According to the documentation "If the previously used view (the view parameter) is adequate, return that." – Rodrigo Morbach May 08 '20 at 17:26
  • In a return function you need to be exhaustive. It would be easier to use a switch statement, which has a `default` case. – nighttalker May 08 '20 at 18:09
  • @nighttalker, a switch statement needs to have a default case and return something, no? Seems like I'd run into the same issue unless I'm misunderstanding. – bruno617 May 09 '20 at 03:10
  • @Rodrigo Morbach, I tried returning pickerView.viewWithTag(Int), and since I have multiple, I need a block of if statements or a switch statement, both require default return which is what I'm struggling with for what that should be. – bruno617 May 09 '20 at 03:23
  • Tried `else if pickerView.tag == 1 { return pickerView.viewWithTag(1)! }` as well as `else if pickerView.tag == 1 { return self.view }` and I get Exception: "layer is a part of cycle in its layer tree" for both. – bruno617 May 09 '20 at 15:32
  • In the `default` case return `UIView()`. – nighttalker May 09 '20 at 16:30

1 Answers1

0

I think it would be best to create a dedicated delegate for each picker, at least for the alarm picker. This will improve your code, because you don't need all these if pickerView == picker1 / picker2 / picker3 / picker4 cascades.

You could also create a base class for the common picker delegate work, and derive a alarm picker delegate with individual code.

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • I like this suggestion, however, I'm very new at this and unfortunately not quite sure how to go about it so I created another question to address it https://stackoverflow.com/q/61698973/4551093 – bruno617 May 09 '20 at 15:14