I am using xcode swift to create a quiz game, and when the game finished, there will be a score which is revealed. The score is added in the quiz view controller, while the score is going to be shown in the next view controller. I am having trouble trying to bring the variable for the score, to the next view controller. Here is my code for the quiz view controller:
class MathsViewController: UIViewController {
let questions = ["What is the turning point of the graph, y = x^2 + 8x + 6 ?", "What are the x-intercepts of the graph, y = x^2 + 11x + 18 ?", "What is the triganomic ratio used when relating the opposite and adjacent?", "What would be the final form of the equation, 14/x = sin(20) ?", "Which equation, would have the steepest slope?", "If we added every positive whole number, what would the solution be?", "Which of the following is the fibonacci sequnce?", "What are the first 10 digits of pi?", "What is the probability of getting a 6 in all your rolls, when rolling a dice 3 times?", "If the hypostenuse of a triangle is 26cm, and a side edge is of 10cm long. What is the length of the other side?"]
let answers = [["(-4, -10)", "(0,0)", "(-6, -1)"], ["x = -2 or x = -9", "x = 18 or x = 11", "x = 9 or x = 2"], ["tan", "cos", "sin"], ["x = 14/sin(20)", "x = 14sin^-1/20", "x = sin^-1(20)"], ["y = 5x+2", "y = 1/3x+4", "y = x+16"], ["-1/12", "infinity", "0"], ["1,1,2,3,5,8,13", "1,2,3,4,5,6,7", "1,1,2,2,4,8,32"], ["3.1415926535", "3.141533536", "3.1415934435"], ["1/216", "1/36", "1/6"], ["24cm", "28cm", "26cm"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement = 0
var points = 0;
//Label
@IBOutlet weak var Label: UILabel!
//Button
@IBAction func action(_ sender: Any)
{
if (sender as AnyObject).tag == Int(rightAnswerPlacement)
{
print("RIGHT!")
points += 1
}
else
{
print("WRONG!!!")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
print(points)
}
override func viewDidLayoutSubviews() {
newQuestion()
}
//Function that displays new question
func newQuestion()
{
Label.text = questions[currentQuestion]
rightAnswerPlacement = Int(arc4random_uniform(3)+1)
//Create Button
var button:UIButton = UIButton()
var x = 1
for i in 1...3
{
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
Here is also the pictures of the view controllers in the storyboard: