Search code examples
swiftswitch-statementuipickerview

Swift 3.0 Multiple Picker Views not recognizing Data Source Variable


My objective is to have 3 pickerViews on one viewController. Each pickerView will list different data sources depending on previously saved settings, which are pulled from a database. I have set up the datasource depending on the value that is pulled from the database with a switch statement shown below.

In the view did load, along with the delegates of the pickers I have set, I have:

switch firstValuePulled {
    case "Animals":
        var pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
        var pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]

    default:
        var pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]

    }

switch second ValuePulled {
    case "Animals":
        var pickerNumber2 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
        var pickerNumber2 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]

    default:
        var pickerNumber2 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]

    }

switch thirdValuePulled {
    case "Animals":
        var pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
        var pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]

    default:
        var pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]

    }

As for picker delegates, the error shows up below "Use of unresolved identifier 'pickerNumber1'", "Use of unresolved identifier 'pickerNumber2'", etc. Could it be that the switch statement was not executed properly and thus the delegates have no values to display the picker?

 func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if (pickerView.tag == 1){
        return pickerNumber1[component].count;
    } else if (pickerView.tag == 2){
        return pickerNumber2[component].count;
    } else {
        return pickerNumber3[component].count;
    }

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {


    if (pickerView.tag == 1){
        return pickerNumber1[component][row]
    } else if (pickerView.tag == 2){
        return pickerNumber2[component][row]
    } else {
        return pickerNumber3[component][row]
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if (pickerView.tag == 1){
        eventOneValue = pickerNumber1[0][pickerView.selectedRow(inComponent: 0)]
        eventOneNumber = pickerView.selectedRow(inComponent: 1) + 1

        print(eventOneValue)
    } else if (pickerView.tag == 2) {
        eventTwoValue = pickerNumber2[0][pickerView.selectedRow(inComponent: 0)]
        eventTwoNumber = pickerView.selectedRow(inComponent: 1) + 1
        print(eventTwoValue)
    } else {
        eventThreeValue = pickerNumber3[0][pickerView.selectedRow(inComponent: 0)]
        eventThreeNumber = pickerView.selectedRow(inComponent: 1) + 1
        print(eventThreeValue)
    }


}    

Solution

  • This is because u have created pickerNumber1, pickerNumber2 locally in the scope of switch statement and U are using in picker delegate! U have to create them in class and set its value in Switch like that

    class YourClassName{

    var pickerNumber1:Array = []

    var pickerNumber2:Array = []

    }

    and then Switch statement use it as

    switch firstValuePulled { case "Animals": pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
       pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
    
    default:
       pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
    
    }
    

    switch second ValuePulled { case "Animals": pickerNumber2 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
        pickerNumber2 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
    
    default:
        pickerNumber2 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
    
    }
    

    switch thirdValuePulled { case "Animals": pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]

    case "Sports":
        pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
    
    default:
        pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
    
    }
    

    and then use them in picker delegates!