代码之家  ›  专栏  ›  技术社区  ›  King

JSON输入Swift的动态表单

  •  0
  • King  · 技术社区  · 7 年前

    我有一个JSON文件,从中我试图用3个页面为Swift创建一个动态的JSON文件,我已经使用Codable将JSON数据传递到一个模型中,并且我可以打印它。这个JSON有3个页面,我想是3个uiviewController。我如何创建这种动态形式,因为我看起来很困惑。这是我到目前为止所尝试的

    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            guard let data = stubbedResponse("forms") else {return}
            do {
                let decoded = try JSONDecoder().decode(BaseModel.self, from: data)
                print(decoded)
            } catch {
    
            }
        }
    
        func stubbedResponse(_ filename: String) -> Data! {
            @objc class VC: NSObject {}
    
            let bundle = Bundle(for: VC.self)
            let path = bundle.path(forResource: filename, ofType: "json")
            return (try? Data(contentsOf: URL(fileURLWithPath: path!)))
        }
    }
    

    杰森:

    {
      "id": "form_1",
      "name": "Pet Adoption Application Form",
      "pages": [
        {
          "label": "Page 1",
          "sections": [
            {
              "label": "Welcome to Pets Rescue",
              "elements": [
                {
                  "type": "embeddedphoto",
                  "file": "https://images.pexels.com/photos/8700/wall-animal-dog-pet.jpg?cs=srgb&dl=animal-collar-dog-8700.jpg&fm=jpg",
                  "unique_id": "embeddedphoto_1",
                  "rules": []
                }
              ]
            },
            {
              "label": "Basic Info",
              "elements": [
                {
                  "type": "text",
                  "label": "Your fullname",
                  "isMandatory": true,
                  "unique_id": "text_1",
                  "rules": []
                },
                {
                  "type": "text",
                  "label": "Email address",
                  "isMandatory": true,
                  "unique_id": "text_2",
                  "rules": []
                },
                {
                  "type": "formattednumeric",
                  "label": "Phone Number",
                  "keyboard": "numeric",
                  "formattedNumeric": "####-###-####",
                  "isMandatory": true,
                  "unique_id": "formattednumeric_1",
                  "rules": []
                },
                {
                  "type": "datetime",
                  "mode": "date",
                  "label": "Date of Birth",
                  "isMandatory": true,
                  "unique_id": "datetime_1",
                  "rules": []
                }
              ]
            }
          ]
        },
        {
          "label": "Page 2",
          "sections": [
            {
              "label": "About your home",
              "elements": [
                {
                  "type": "yesno",
                  "label": "Do you have a yard?",
                  "isMandatory": true,
                  "unique_id": "yesno_1",
                  "rules": [
                    {
                      "condition": "equals",
                      "value": "Yes",
                      "action": "show",
                      "otherwise": "hide",
                      "targets": [
                        "text_3"
                      ]
                    }
                  ]
                },
                {
                  "type": "text",
                  "label": "Is it fenced? Also indicate the type",
                  "isMandatory": false,
                  "unique_id": "text_3",
                  "rules": []
                }
              ]
            }
          ]
        },
        {
          "label": "Page 3",
          "sections": [
            {
              "label": "Additional Information",
              "elements": [
                {
                  "type": "text",
                  "label": "Please provide your veterinarian's name",
                  "isMandatory": false,
                  "unique_id": "text_4",
                  "rules": []
                },
                {
                  "type": "text",
                  "label": "Please provide the name of a personal reference",
                  "isMandatory": true,
                  "unique_id": "text_5",
                  "rules": []
                }
              ]
            }
          ]
        }
      ]
    }
    

    我理解TableView也可以在这个实例中与一个表示下一页的按钮一起使用。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shehata Gamal    7 年前

    你有这种结构

    struct BaseModel {  
     let pages: [Model] 
    }
    

    你将有一个VC,你应该像

    class SomeVC:UIViewController {
      var currentIndex = 0
      var pages = [Model]()
    }
    

    如果你说一个表视图是在那里构建的,那么在下一个页面操作中

    let next = currentIndex + 1
    if next < pages.count {
      let vc = SomeVC()
      vc.currentIndex = next
      vc.pages = pages
     // push the vc
    }
    else {
     // no more
    }
    

    此外,您还可以将此数组设置为单个