Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Rasmus Akselsen
Rasmus Akselsen
4,056 Points

Creating levels/ Generate levels

Hello,

I am in the process of making an question-app in Swift. And I have made much of it, but I'm missing an important part. Namely, being able to generate new questions to the user without having to make a View Controller for each question. What I want is, therefore, only to have one View Controller, consisting of 3 labels and 3 buttons, and then load new questions to the View Controller.

Are there any good suggestions for where I can learn/read about this, or some suggestions on how i should handle this problem?.

Hope my question makes sense, otherwise please write and ask into it.

/Rasmus

2 Answers

Rasmus,

Why not create a struct, then create a list of questions. After you have completed that you can create an array from the questions. Then create a function that will loop through the array. You can do this is a few ways.. below is a simple example:

struct Question {
  var askQuestion: String
  var answer: String
}

var firstQuestion = Question(askQuestion: "When was swift released?", answer: "June 2, 2014")
var secondQuestion = Question(askQuestion: "Who Designed Swift?", answer: "Chris Lattner & many other programmers")
var thirdQuestion = Question(askQuestion: "When was Swift 1.1 released?", answer: "October 22, 2014")
var forthQuestion = Question(askQuestion: "True or False: Swift and Objective-C code can be used in a single program", answer: "True!")
var fifthQuestion = Question(askQuestion: "Swift uses what to manage memory.", answer: "Automatic Reference Counting (ARC)")
var sixthQuestion = Question(askQuestion: "Use ______ to make a constant", answer: "let")
var seventhQuestion = Question(askQuestion: "Use ______ to make a variable", answer: "var")

var studyQuestions = [firstQuestion, secondQuestion, thirdQuestion, forthQuestion,fifthQuestion, sixthQuestion, seventhQuestion]
var questionIndex = -1


func nextQuestion() -> Question {

    questionIndex++

    if questionIndex == studyQuestions.endIndex {

        questionIndex = 0
    }

    println(questionIndex)
    return studyQuestions[questionIndex]

}
Rasmus Akselsen
Rasmus Akselsen
4,056 Points

Wow, that worked like a charm :) Thanks for the quick reply.

Glad to help! Good luck with the app.. let me know when its done so I can download it :)