0

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.)

  • 1
    It's Objective-C, but the concepts are identical in Swift: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers. In short pass data from first to second view controller in `prepareForSegue` and pass data back using either delegate-protocol pattern or in `prepareForSegue` in an unwind segue. – Rob Sep 13 '16 at 17:49
  • There is a topic about [Passing data between View Controllers](http://stackoverflow.com/documentation/ios/434/passing-data-between-view-controllers#t=201609131753185051819) in documentation. This might help you. – FelixSFD Sep 13 '16 at 17:54
  • Why is this marked a duplicate when the OP specifically asked how to do it in Swift but the "duplicate" answer is in Objective-C? Yes, conceptually it's the same but not everyone can read Objective-C well enough to convert it to Swift, especially beginners. – Jim Sep 13 '16 at 18:06
  • As Jim has said, 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. – EllaHiggins Sep 13 '16 at 19:10
  • There are also a lot of Swift answers in the linked topic. There are also a lot of questions and answers in the *Related* column. – vadian Sep 13 '16 at 19:16

0 Answers0