不同的视图控制器加载相同的文本

时间:2019-02-07 03:42:03

标签: ios arrays swift viewcontroller

我通常是敏捷和编程方面的初学者,我正在尝试制作一个测验应用程序,我希望该应用程序从开始屏幕转到选择菜单,然后再进入测验屏幕。

问题是当我按下第二个类别按钮时,它将带我到正确的视图控制器。但是它从第一个数组输出相同的问题和答案。我尝试将变量和数组设为私有,但这仍然行不通。

我在做什么错了?

> //StartScreen ViewContoller
    import UIKit

class ViewController: UIViewController {

    @IBAction func startButton(_ sender: Any) {
        self.performSegue(withIdentifier: "StartToMenuSegue", sender: self)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
}


> //Menu ViewController
import UIKit

class MenuViewController: UIViewController {

    @IBAction func firstCategoryButton(_ sender: Any) {
        self.performSegue(withIdentifier: "MenuToQuestions1Segue", sender: self)

    }
    @IBAction func secondCategoryButton(_ sender: Any) {
         self.performSegue(withIdentifier: "MenuToQuestions2Segue", sender: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
}


> //FirstCategoryQuestions
import UIKit

class Questions1ViewController: UIViewController {

        private let questions1 = [
           "Who created this app?", 
           "What was the 2017-2018 FBLA-PBL National Theme?", 
           "In what year did NYS FBLA recieve its National Charter?", 
           "Which of the following is a responsibility of ALL state officers?", 
           "One of the key responsibilites of the State Historian is:", 
           "Which of the following is a TRUE statement:"
        ]

        private let answers1 = [
           ["Abdullah Bismil", "Duncan Sheen", "Avinash Gagai", "Christian Pascal"], 
           ["Elevate Your Future", "A Legacy of Leadership", "Suit Up, Step Up",""], 
           ["1964", "1960", "1972", "1972"], 
           ["Compile and submit monthly correspondence", "Write and distribute the minutes of state officer meegins", "Plan and organize the Fall district meeting", ""], 
           ["Create and publish a State Annuel Report", "Evaluate state vice presidents at designated times", "Review all reimbursement requests", ""], 
           ["Only 3 candidates can run for any one election posistion", "Non-dues paying FBLA members are eligible to hold state office", "No approval is necessary for a FBLA member to run for state office", ""]
        ]

        //variables
        private var currentQestion1 = 0
        private var rightAnswerPlacement1: UInt32 = 0
        private var correct1 = 0
        private var incorrect1 = 0

        @IBOutlet weak var questionView: UILabel!

        @IBAction func answerButtons(_ sender: AnyObject) {
           if (sender.tag == Int(rightAnswerPlacement1)) {
              print ("CORRECT")
              correct1 += 1
           } else {
              print ("INCORRECT")
              incorrect1 += 1
           }

           if (currentQestion1 != questions1.count) {
              newQuestion1()
           } else if (currentQestion1 == questions1.count) {
              killCommand1()
           }
       }

       override func viewDidAppear(_ animated: Bool) {
          newQuestion1()
       }

       //function that displays new question
       func newQuestion1() {
          questionView.text = questions1[currentQestion1]

          rightAnswerPlacement1 = arc4random_uniform(4) + 1

          //create a button
          var button:UIButton = UIButton()

          var x = 1
          for i in 1...4 {
             //create a button
             button = view.viewWithTag(i) as! UIButton

             if (i == Int(rightAnswerPlacement1)) {
                button.setTitle(answers1[currentQestion1][0], for: .normal)
             } else {
                button.setTitle(answers1[currentQestion1][x], for: .normal)
                x += 1
             }
          }
          currentQestion1 += 1
       }

       func killCommand1() {
          self.performSegue(withIdentifier:"Questions1ToStartSegue", sender: self)
       }

       override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
       } 
}

> //Second Category ViewController
import UIKit

class Questions2ViewController: UIViewController {

    private let questions2 = ["Hello how Are you?", "What is my favorite color"]

    private let answers2 = [["Good", "Fine", "OK", "Eh"], ["Blue", "Green", "Red", "Purple"]]

   //variables
   private var currentQestion2 = 0
   private var rightAnswerPlacement2:UInt32 = 0
   private var correct2 = 0
   private var incorrect2 = 0

   @IBOutlet weak var questionView2: UILabel!  

   @IBAction func answerButtons2(_ sender: AnyObject) {
      if (sender.tag == Int(rightAnswerPlacement2)) {
         print ("CORRECT")
         correct2 += 1
      } else {
         print ("INCORRECT")
         incorrect2 += 1
      }

      if (currentQestion2 != questions2.count) {
         newQuestion2()
      } else if (currentQestion2 == questions2.count) {
         killCommand2()
      }
  }

  override func viewDidAppear(_ animated: Bool) {
     newQuestion2()
  }

  //function that displays new question
  func newQuestion2() {
     questionView2.text = questions2[currentQestion2]

     rightAnswerPlacement2 = arc4random_uniform(4) + 1

     //create a button
     var button: UIButton = UIButton()

     var x = 1
     for i in 1...4 {
        //create a button
        button = view.viewWithTag(i) as! UIButton

        if (i == Int(rightAnswerPlacement2)) {
           button.setTitle(answers2[currentQestion2][0], for: .normal)
        } else {
           button.setTitle(answers2[currentQestion2][x], for: .normal)
           x += 1
        }
     }
     currentQestion2 += 1
  }

  func killCommand2() {
     self.performSegue(withIdentifier:"Questions1ToStartSegue", sender: self)
  }

  override func viewDidLoad() {
     super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
  }  

}

0 个答案:

没有答案