创建一个静态下拉菜单

时间:2018-10-29 01:18:04

标签: ios arrays swift drop-down-menu

我想要一个下拉菜单,该菜单根据用户的选择而变化。

例如:

let first_Field = ["-", "new recruits", "police officer", "PSO"] 
let second_field = ["-", "Training", "Courts", "Others"]

第三字段应基于first_Field选择

let third_field = []

因此,我当时认为它可能适用于if条件,但我并不完全知道该怎么做。

例如:

let NR = "new recruits"
let NRtraining = ["Training1", "Training2"]

let first_Field = ["-", NR] 
let second_Field = ["training", "Tester", "School"]
if (first_Field == NR && second_Field == "training") { 
     //user selected NR and training
    //third_Field should show array of NRtraining
}

请帮助?

1 个答案:

答案 0 :(得分:0)

您应该更改数据结构,例如:

   {
       "new recruits" : {
               "Training" : ["Training1", "Training2"],
               "Courts" : ["Court1","Court2"],
               "Others" : ["abc","def"]
       },
       "police officer" : {
               "Training" : ["Training1", "Training2"],
               "Courts" : ["Court1","Court2"],
               "Others" : ["abc","def"]
       },
      "PSO" : {
               "Training" : ["Training1", "Training2"],
               "Courts" : ["Court1","Court2"],
               "Others" : ["abc","def"]
       }

    }

在选择新兵后,您可以通过“培训,法院,其他人员”键更改第二个下拉菜单;在选择第二个下拉菜单时,您可以使用培训,法院等方面的价值进行第三次下拉菜单

快捷代码:

      let mainDict =  [
            "new recruits" :  [
                "Training" : ["Training1", "Training2"],
                "Courts" : ["Court1","Court2"],
                "Others" : ["abc","def"]
            ],
            "police officer" : [
                "Training" : ["Training1", "Training2"],
                "Courts" : ["Court1","Court2"],
                "Others" : ["abc","def"]
            ],
            "PSO" : [
                "Training" : ["Training1", "Training2"],
                "Courts" : ["Court1","Court2"],
                "Others" : ["abc","def"]
            ]

        ]

        let firstDropArray =  ["-", "new recruits", "police officer", "PSO"]
        var second_field = ["-", "Training", "Courts", "Others"]

        // on select first Dropdown array store value of selected item here i select "new recruits"
        let firstSelect = firstDropArray[1]

        // on select second Dropdown array store value of selected item here i select "Courts"
        let seconSelect = second_field[2]

        // third array value

        let thirdArray = mainDict[firstSelect]![seconSelect]
        print(thirdArray)