So i have a quiz and when the quiz ends i want to show how many answers were correct. Here is how i have tryed to do it.
This is the first VC
import UIKit
class QuizViewController: UIViewController {
@IBOutlet weak var timer: UILabel!
@IBOutlet weak var question: UILabel!
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var prasanje: UILabel!
//odgovori
@IBOutlet weak var odgovor1: UIButton!
@IBOutlet weak var odgovor2: UIButton!
@IBOutlet weak var odgovor3: UIButton!
let allQuestions = QuestionBank()
let randomQuestions = QuestionBank.shared.getRandomQuestions(6)
var questionNumber: Int = 0
var score: Int = 0
var selectedAnswer: Int = 0
let updateT = EndViewController()
var taimer:Timer?
var seconds: Int = 60
override func viewDidLoad() {
super.viewDidLoad()
updateQuestion()
updateUI()
}
@IBAction func Back(_ sender: Any) {
let dialogMessage = UIAlertController(title: "Потврдете", message: "Дали сте сигурни дека сакате да прекинете?", preferredStyle: .alert)
let ok = UIAlertAction(title: "Да", style: .default, handler: { (action) -> Void in
print("Ok button tapped")
self.dimiss()
})
let cancel = UIAlertAction(title: "Откажи", style: .cancel) { (action) -> Void in
print("Cancel button tapped")
}
dialogMessage.addAction(ok)
dialogMessage.addAction(cancel)
self.present(dialogMessage, animated: true, completion: nil)
}
func dimiss(){
self.dismiss(animated: true, completion: nil)
}
@IBAction func odgovorpotvrden(_ sender: UIButton) {
if sender.tag == selectedAnswer {
print("correct")
score += 1
}else {
print("wrong")
}
questionNumber += 1
updateQuestion()
}
func updateQuestion() {
if questionNumber <= randomQuestions.count - 1{
image.image = UIImage(named:(randomQuestions[questionNumber].image))
prasanje.text = randomQuestions[questionNumber].prasanje
odgovor1.setTitle(randomQuestions[questionNumber].odgovor1, for: UIControl.State.normal)
odgovor2.setTitle(randomQuestions[questionNumber].odgovor2, for: UIControl.State.normal)
odgovor3.setTitle(randomQuestions[questionNumber].odgovor3, for: UIControl.State.normal)
selectedAnswer = randomQuestions[questionNumber].tocenodgovor
}
else {
endVC()
}
updateUI()
}
func updateUI() {
question.text = "\(questionNumber)/\(randomQuestions.count)"
let min = (seconds / 60) % 60
let sec = seconds % 60
timer!.text = String(format: "%02d", min) + ":" + String(format: "%02d", sec)
time()
}
func time() {
if taimer == nil {
taimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
if self.seconds == 0{
self.timeup()
}else if self.seconds <= 60 {
self.seconds -= 1
self.updateUI()
}
}
}
}
func timeup() {
taimer?.invalidate()
taimer = nil
let timeup = UIAlertController(title: "Времето истече", message: "Крај на испитот. Дали сакате да започнете од почеток?", preferredStyle: .alert)
let restart = UIAlertAction(title: "Ресетирај", style: .default, handler: { (action) -> Void in
print("restart button tapped")
self.restartQuiz()})
let dismiss = UIAlertAction(title: "Откажи", style: .default, handler: { (action) -> Void in
print("dismiss button tapped")
self.dimiss()})
timeup.addAction(restart)
timeup.addAction(dismiss)
self.present(timeup, animated: true, completion: nil)
}
func updateScore(){
if score > randomQuestions.count - 2 {
updateT.no1 = "Cestitki"
}else {
updateT.no1 = "uaa"
}
}
func restartQuiz() {
seconds = 60
score = 0
questionNumber = 0
updateQuestion()
}
@IBAction func endVC() {
let sb = UIStoryboard(name: "Main", bundle: nil)
if let endVC = sb.instantiateViewController(identifier: "endVC") as? EndViewController{
self.present(endVC, animated: true, completion: nil)
}
}
And here is the VC that im trying to pass the data to.
import UIKit
class EndViewController: UIViewController {
@IBOutlet weak var cestitki: UILabel!
@IBOutlet weak var skor: UILabel!
var no1: String = ""
var no2: String = ""
override func viewDidLoad() {
super.viewDidLoad()
cestitki!.text = no1
}
But when i run the program the label is blank and there is nothing.
Im either trying to pass the "score" value form one to the other VC or change the label from the first VC in the second. Both helps