Click here to Skip to main content
15,667,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im making a simple question with answer type app, and i want there to be a reset button that brings me back to the first question each times its pressed.

<pre lang="Swift"><pre>
import UIKit

struct Question {
    var imageName: String
    let question: String
    let answer: String

class ViewController: UIViewController {

    @IBOutlet weak var question: UILabel!

    @IBOutlet weak var answer: UILabel!

    @IBOutlet weak var imgQ: UIImageView!
    private var questions = [Question(imageName: "me",question: "What is your name?", answer: "name"),
                             Question(imageName: "colour", question: "What is your favourite colour", answer: "colour"),
                             Question(imageName: "movie", question: "What is your favourite movie?", answer: "movie"),
                             Question(imageName: "music", question: "What is your favourite music genre?", answer: "music")]
    private var currentQuestionIndex = 0
    private var shouldShowNextQuestion = false

    override func viewDidLoad() {
        // Do any additional setup after loading the view.
        imgQ.isHidden = true

    @IBAction func showQuestion(_ sender: Any) {
        print("show question button pressed")
        if shouldShowNextQuestion {
            shouldShowNextQuestion = false
            // If currentQuestionIndex is greater than the array, start from the beginning
            currentQuestionIndex = currentQuestionIndex >= questions.count - 1 ? 0 : currentQuestionIndex + 1
        question.text = questions[currentQuestionIndex].question
        answer.text = ""

        if imgQ.isHidden == false {
            imgQ = questions[currentQuestionIndex].imageName

    @IBAction func showAnswer(_ sender: Any) {
        print("show answer button pressed")
        shouldShowNextQuestion = true
        question.text = questions[currentQuestionIndex].question
        answer.text = questions[currentQuestionIndex].answer
    @IBAction func resetButton(_ sender: Any) {
        print("reset button pressed")
        // get the questions to start over when button pressed (??)

What I have tried:

ive tried setting the index back to 0 but when i do that it doesnt work for me.
Updated 25-Jan-21 20:48pm

1 solution

it doesnt work for me

This is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

And in this case, start with the debugger: Apple Debugging Tools[^] and look at what happens when you do set the index "back to zero".

Testing and debugging your code are a big part of development: it's not unusual to spend 10% of your time writing code, and 90% making it work correctly! So while you are still dealing with tiny little apps like this one, that's the perfect time to get familiar with the tools you will need for bigger, more complex tasks. Debugging is a skill - and like all skills it has to be used in order to develop. Just going "it doesn't work" and expecting others to fix it for you isn't going to help you at all in the long run, even if it gets you out of a hole in the short term!
Give it a try, and see how much you can find out!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900