我无法将某些变量从第一个视图控制器传递到第二个视图控制器(称为ScoreViewController)。我希望能够从第二个视图控制器访问的变量是CorrectAnswerTotal和QuestionsAskedTotal。提前谢谢。
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()
}
(我问这个问题,因为我不熟悉Objective-C和使用segues背后的概念,而且在编程方面是一个完全的初学者。我只是发布了这个问题,因为我无法理解之前的问题这类问题的答案,如果可能的话,希望特别为我的代码提供帮助。)