hkokay
hkokay

Reputation: 57

how to make a button show different labels each time its pressed

I am trying to make a simple question with corresponding answer app on swift. I've created the questions and the answers but I'm not sure how to show them once the buttons are pressed. When ever the button is pressed a different question should show and the corresponding answer should show once the answer button is pressed.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var question: UILabel!
    
    @IBOutlet weak var answer: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let question1 = "What is your name?"
        let answer1 = "name"
        
        let question2 = "What is your favourite colour"
        let answer2 = "colour"
        
        let question3 = "What is your favourite movie?"
        let answer3 = "movie"
        
        let question4 = "What is your favourite music genre?"
        let answer4 = "genre"
    }

    @IBAction func showQuestion(_ sender: Any) {
        print("show question button pressed")
    }
    
    @IBAction func showAnswer(_ sender: Any) {
        print("show answer button pressed")
    }
}

Upvotes: 0

Views: 82

Answers (2)

πter
πter

Reputation: 2217

It is a good practice to represents your questions as objects so it is more scalable(see Question struct). You can use a variable currentQuestionIndex in the code, which will track the current question, and on every new press on the question button, it will increase that value until it reaches the end of the array, when it restarts.

import UIKit

struct Question {
    let question: String
    let answer: String
}

class ViewController: UIViewController {

    @IBOutlet weak var question: UILabel!

    @IBOutlet weak var answer: UILabel!

    private let questions = [Question(question: "What is your name?", answer: "name"),
                             Question(question: "What is your favourite colour", answer: "colour"),
                             Question(question: "What is your favourite movie?", answer: "movie"),
                             Question(question: "What is your favourite music genre?", answer: "genre")]
    private var currentQuestionIndex = 0
    private var shouldShowNextQuestion = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func showQuestion(_ sender: Any) {
        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 = ""
    }

    @IBAction func showAnswer(_ sender: Any) {
        shouldShowNextQuestion = true
        question.text = questions[currentQuestionIndex].question
        answer.text = questions[currentQuestionIndex].answer
    }


    @IBAction func reset(_ sender: Any) {
         shouldShowNextQuestion = false
         currentQuestionIndex = 0
         showQuestion(self)
    }

}

Upvotes: 1

Shima Daryabari
Shima Daryabari

Reputation: 54

no need to add extra label just do the following:

import UIKit

struct Question {
let question: String
let answer: String
}
class ViewController: UIViewController {
@IBOutlet weak var question: UIButton!
@IBOutlet weak var answer: UIButton!

  let questions = [Question(question: "What is your name?", answer: "name"),
                         Question(question: "What is your favourite colour", answer: "colour"),
                         Question(question: "What is your favourite movie?", answer: "movie"),
                         Question(question: "What is your favourite music genre?", answer: "genre")]
private var questionIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()
question.setTitle(questions[questionIndex].question, for: .normal)
}

@IBAction func showQuestion(_ sender: UIButton) {
    if questionIndex < questions.count {

        questionIndex = questionIndex + 1
    } else {
questionIndex = 0
}
 question.setTitle(questions[questionIndex].question, for: .normal)
 answer.setTitle("", for: .normal)

}

@IBAction func showAnswer(_ sender: UIButton) {
answer.setTitle(questions[currentQuestionIndex].answer, for: .normal)
}
}

Upvotes: 0

Related Questions