我有一个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也可以在这个实例中与一个表示下一页的按钮一起使用。