1

I should create Quiz app, I just created template and MVC, but cannot understand how to pass data from array with question and answers to label on screen ? I have 3 screen 1.Screen with "start" button 2.Main screen where I should pass data from array 3.Screen - result

I attached template screen enter image description here

struct Question {
    var text: String
    var answers: [Answer]
}

extension Question {
     static func loadData() -> [Question] {
        return [
            Question(
                text:some text",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text ?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "Сsome text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            ),
            Question(
                text:"some text?",
                answers: [
                    Answer(text: "some text", type: .Sloupok),
                    Answer(text: "some text", type: .Pikachy),
                    Answer(text: "some text", type: .Lucario),
                    Answer(text: "some text", type: .Slugma),
                    Answer(text: "some text", type: .Vaperon)
                ]
            )
        ]
    }
}
AlexLeo
  • 79
  • 1
  • 9
  • Use Collection View for showing question and answers – RajeshKumar R May 05 '19 at 10:23
  • But I have stackView label with switch! I will have five questions and answer options, where I have to choose the appropriate one, move the switch to position “on”and press the “reply” button, to go to the next question – AlexLeo May 05 '19 at 12:22
  • Create a var currenetQuestion = 0 and update questoin, answers from the array. Once reply button is pressed increment currentQuestion and update question and answers – RajeshKumar R May 05 '19 at 14:32

1 Answers1

0

You can set data to labels one by one, but it is not good solution. If you need dynamic number of answers, it becomes complex to pass data.

I suggest to use TableView. You can create custom cell for displaying answer and use it in your tableview. Here is good example of using TableView: UITableView example for Swift

levan
  • 330
  • 6
  • 17
  • You need to have references of all labels in your ViewController and set text one by one, something like that: @IBOutlet weak var lbl: UILabel! self.lblTitle.text = "blahblah" – levan May 05 '19 at 12:34