1

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.

  • Some may require TextField as to submit the answer (with validations on input),
  • Some of may have radio buttons (with YES and NO)
  • Some will have Checkbox
  • Some have dropdown with dynamic values

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?

iLoveIOS
  • 33
  • 7
  • Keep it simple first! if questions count is huge, use different cells in UITableView else use custom view in UIStackView. – king wang Sep 02 '21 at 08:48
  • @kunwang QuestionCount will be somewhere 5 to 10 on each screens. But my query is, how can I get the answers added by the user? and if there are child questions, how do I need to handle ? Child question mean > If Parent question is of type Radio button (YES/NO), and user selects 'YES', then a child question should get appear which again can be of different types(i.e. InputField, Date, Dropdown, etc) – iLoveIOS Sep 02 '21 at 09:14
  • You need unique id for each question and answer. And other details settings of your child UI's (Button, TextFIeld and etc). Hope you getting all those details from server. – Abdul Hoque Nuri Sep 02 '21 at 09:24
  • @AbdulHoqueNuri Yes that'll be received from server. Only thing I am not getting is, how to get (read) user's input for each question. I mean if my questionType is RadioButton with YES and NO Option, if user selects YES, then I need to display one new question (let's its question type is InputText,) and need to read user's provided answer of that child question – iLoveIOS Sep 02 '21 at 09:44
  • Yes, You need to design those UI formate. I mean pre define UI's for your questions. Those Ui's you can call as layouts and call/reload according to user selections. – Abdul Hoque Nuri Sep 02 '21 at 10:13

1 Answers1

2

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
schmidt9
  • 4,436
  • 1
  • 26
  • 32