I am trying to create a quiz app that shows a questions and then the users will select the answer by pressing a one of tow button [ true / false ] and then press next button to move to the next question.
I want to hide the answer buttons or disable them after the users selects one of them, and then the nextButton appears.
the two answers buttons is connected with the same IBAction but have different tags [ 1 for the true button and 2 for the false button ], the button (next) connected with another IBAction.
the problem that i can't access button properties !!
it tried this
answerPressed.isEnabled = false
but every time i print the button name followed by a dot i have no suggestion , xcode give me error " Expected member name following '.' " the only suggestion i have is this :
answerPressed(sender: AnyObject)
here is the full code for the app , any help pleas :)
import UIKit
class ViewController: UIViewController {
let allQuestion = QuestionBank()
var questionNumber = 0
var score = 0
var selectedAnswer = true
@IBOutlet weak var checkAnswerLabel: UILabel!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet var progressBar: UIView!
@IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
startOver()
}
@IBAction func answerPressed(_ sender: AnyObject) {
if sender.tag==1{
selectedAnswer = true
}
else if sender.tag == 2 {
selectedAnswer = false
}
let correctAnswer = allQuestion.list[questionNumber].answer
if correctAnswer == selectedAnswer {
checkAnswerLabel.text = "Right 😃"
score += 1
scoreLabel.text = "score : \(score)"
}
else if correctAnswer != selectedAnswer {
checkAnswerLabel.text = "Wrong 😥"
answerPressed(<#T##sender: AnyObject##AnyObject#>)
}
}
@IBAction func nextButtonPressed(_ sender: UIButton) {
if questionNumber < 12 {
questionNumber += 1
questionLabel.text = allQuestion.list[questionNumber].question
checkAnswerLabel.text = ""
progressLabel.text = "\(questionNumber+1)/13"
}
else {
let alert = UIAlertController(title: "Great 👍", message: "you have rech the end of the quiz, do you want to restart", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: ({
UIAlertAction in
self.startOver()
}))
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func startOver () {
questionNumber = 0
score = 0
print(questionNumber)
questionLabel.text = allQuestion.list[questionNumber].question
scoreLabel.text = "score: \(score)"
progressLabel.text = "\(questionNumber+1)/13"
checkAnswerLabel.text = ""
}
}
thanks
Apologies if I've misunderstood the question. It sounds as if you want to disable the true and false buttons after one of them is pressed. However, I don't see either of them in your list of IBOutlet variables. You can't reference them directly because they aren't known to the class.
In the answerPressed function you could use the sender reference to disable the sender (true or false button) but you wouldn't be able to disable the one that wasn't selected.
I suspect all you need to do is create IBOutlets for your true and false buttons, map them to the view and then you can do what you like with them.