I am having trouble passing certain variables from my first view controller to my second (called ScoreViewController). The variables I want to be able to access from the second view controller are CorrectAnswerTotal and QuestionsAskedTotal. Thanks in advance.
import UIKit
struct Question {
var Question : String
var Answers : [String]!
var Answer : Int!
}
class ViewController: UIViewController {
@IBOutlet var QuestionLabel: UILabel!
@IBOutlet var Buttons: [UIButton]!
var Questions = [Question]()
var QuestionNumber = Int()
var AnswerNumber = Int()
var CorrectAnswerTotal = 0
var QuestionsAskedTotal = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Questions = [Question (Question: "What is my favourite colour?", Answers: ["Red", "Yellow", "Blue", "Green"], Answer: 2),Question (Question: "What is my name?", Answers: ["Ella", "Jane", "Rachel", "Ellie"], Answer: 0), Question (Question: "What is my dads name?", Answers: ["John", "Steve", "Peter", "David"], Answer: 1), Question (Question: "What is my house number?", Answers: ["23", "199", "3", "104"], Answer: 3)]
pickQuestion()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickQuestion(){
if Questions.count > 0 {
QuestionNumber = random () % Questions.count
QuestionLabel.text = Questions[QuestionNumber].Question
AnswerNumber = Questions[QuestionNumber].Answer
for i in 0..<Buttons.count{
Buttons[i].setTitle(Questions[QuestionNumber].Answers[i], forState: UIControlState.Normal)
}
Questions.removeAtIndex(QuestionNumber)
}
else{
NSLog("All Questions Asked")
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
}
@IBAction func Button1Action(sender: AnyObject) {
if AnswerNumber == 0 {
CorrectAnswerTotal += 1
NSLog("You are correct %d" , CorrectAnswerTotal)
}
else{
NSLog("You are wrong")
}
QuestionsAskedTotal += 1
pickQuestion()
}
@IBAction func Button2Action(sender: AnyObject) {
if AnswerNumber == 1 {
CorrectAnswerTotal += 1
NSLog("You are correct %d" , CorrectAnswerTotal)
}
else{
NSLog("You are wrong")
}
QuestionsAskedTotal += 1
pickQuestion()
}
@IBAction func Button3Action(sender: AnyObject) {
if AnswerNumber == 2 {
CorrectAnswerTotal += 1
NSLog("You are correct %d" , CorrectAnswerTotal)
}
else{
NSLog("You are wrong")
}
QuestionsAskedTotal += 1
pickQuestion()
}
@IBAction func Button4Action(sender: AnyObject) {
if AnswerNumber == 3 {
CorrectAnswerTotal += 1
NSLog("You are correct %d" , CorrectAnswerTotal)
}
else{
NSLog("You are wrong")
}
QuestionsAskedTotal += 1
pickQuestion()
}
(I am asking this question as I am unfamiliar with Objective-C and the concepts behind using segues and am a complete beginner when it comes to programming in general. I have only posted this question as I couldn't understand the previous answers to this type of question and was hoping for help specifically for my code if possible.)