Reputation: 33
I have one requirement in which server will provide us list of screens, and each screen will have certain set of questions.
These question may vary in their question types. i.e.
From mobile side (iOS), we'll need to get these questions's from Server, dump it into local database and then display a list questions (in Form) based on Screen and the questionType.
Please suggest a good approach !
One way I thought to create custom-views for each question type, i.e. reusable view for radioButton, reusableView for TextInput, etc.
And add all these views into a stack view and display it.
But, problem is how to keep reference of all these added views? I mean if there are 3 questions of type "inputTextfield", we can render a reusable view which contains Question Label, and UITextfield (to submit answer). But how to get values (answer) from each component and submit all the questions and their answers in a form?
Upvotes: 1
Views: 81
Reputation: 4538
Here is a simplified starting solution how you could bind your questions to views and get views values. First you map your data to models and then you should implement mechanism how you update answers inside entries (for instance based on control state change).
import UIKit
struct Question {
var text: String
var type: EntryType
/// use this array to output conditional nested questions
var subquestions: [Question]
}
struct Answer {
var text: String
}
enum EntryType {
case text
case radio
case checkbox
case dropdown
}
class Entry {
var question: Question
/// Set this field on text / state change of control
var answer: Answer?
var type: EntryType
init(question: Question, type: EntryType) {
self.question = question
self.type = type
}
}
struct CustomView {
/// Assign this while views creation / output to bind entry and view and to update entry
var entry: Entry
}
var entries = [Entry]() // TODO: populate array
// ... populate entries using answers and create views based on type
Upvotes: 2