使用segue将数据从Model移到目标ViewController

时间:2018-09-02 01:41:44

标签: swift

我一般都不熟悉Swift和编程。我正在构建测验应用程序。该应用程序使用TopicsViewController选择一个主题,然后选择到QuestionsViewController。各种主题的问题存储为单独的swift对象文件。当我在TopicsViewController中按topic1按钮选择到QuestionsViewController时,我想选择Topic1问题文件。我想知道在选择到QuestionsViewController时选择特定主题时如何选择特定问题文件QuestionBank1 / QuestionBank2? Navigation Pane Main.Storyboard     导入UIKit     类TopicsViewController:UIViewController,returnToTopicVCDelegate {

        func goToTopicVC() {
        }

        override func viewDidLoad() {
            super.viewDidLoad()
        }

        @IBAction func goToQuestionsVCWhenPressed(_ sender: UIButton) {
            performSegue(withIdentifier: "segueToQuestionVC", sender: self)
        }

          override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "segueToQuestionVC" {
                let quizVC = segue.destination as! QuizViewController
                quizVC.delegate = self
        }
    }
    }

    import UIKit
    import QuartzCore

    protocol returnToTopicVCDelegate{
        func goToTopicVC()
    }

    class QuizViewController: UIViewController {

        var delegate : returnToTopicVCDelegate?

        //outlet for the question label and image view
        @IBOutlet weak var questionLabel: UILabel!
        @IBOutlet weak var questionImageView: UIImageView!
        //outlet for the buttons
        @IBOutlet weak var optionAButton: UIButton!
        @IBOutlet weak var optionBButton: UIButton!
        @IBOutlet weak var optionCButton: UIButton!
        @IBOutlet weak var optionDButton: UIButton!
        @IBOutlet weak var optionEButton: UIButton!
        //outlets for the progress
        @IBOutlet weak var questionCounter: UILabel!
        @IBOutlet weak var scoreLabel: UILabel!

        var allQuestions = QuestionBank()
        var selectedAnswer : Int = 0 // answer selected by the subject
        var questionNumber: Int = 0
        var score: Int = 0

    // functions executed after an answer is picked 
        @IBAction func answerPressed(_ sender: UIButton) {
            if sender.tag == selectedAnswer {
                print("correct answer")
                sender.backgroundColor = .green
                score += 1
            } else {
                print("wrong")
                sender.backgroundColor = .red
                print("\(allQuestions.list[questionNumber].correctAnswer)")
                //the following two lines change the right answer button to green using the tag value of the button
                let correctAnswerButton = view.viewWithTag(selectedAnswer) as? UIButton
                correctAnswerButton?.backgroundColor = UIColor.green
            }
        }

        @IBAction func GoToNextQuestion(_ sender: UIButton) {
            questionNumber += 1
            nextQuestion()
        }

    // selects a new questions and updates the score

        func nextQuestion(){
            if questionNumber <= allQuestions.list.count - 1 {
            questionLabel.text = allQuestions.list[questionNumber].question
            questionImageView.image = UIImage(named: (allQuestions.list[questionNumber].questionImage))
            optionAButton.setTitle(allQuestions.list[questionNumber].optionA, for: UIControlState.normal)
            optionBButton.setTitle(allQuestions.list[questionNumber].optionB, for: UIControlState.normal)
            optionCButton.setTitle(allQuestions.list[questionNumber].optionC, for: UIControlState.normal)
            optionDButton.setTitle(allQuestions.list[questionNumber].optionD, for: UIControlState.normal)
            optionEButton.setTitle(allQuestions.list[questionNumber].optionE, for: UIControlState.normal)
                      selectedAnswer = allQuestions.list[questionNumber].correctAnswer
                updateUI()
            } else {
                let alert = UIAlertController(title: "Great!", message: "Do you want to start over?", preferredStyle: .alert)
                let restartAction = UIAlertAction(title: "Restart", style: .default) {(UIAlertAction) in
                    self.restartQuiz()
                }
                alert.addAction(restartAction)
                present(alert, animated: true, completion: nil)
            }
        }


        func updateUI(){
            scoreLabel.text = "score: \(score)"
            questionCounter.text = "\(questionNumber + 1)/\(allQuestions.list.count)"
        }

        func restartQuiz(){
            score = 0
            questionNumber = 0
            nextQuestion()
        }


        @IBAction func goBackToTopicsVC(_ sender: Any) {
            delegate?.goToTopicVC()
            dismiss(animated: true, completion: nil)
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用以下步骤:

1-将TopicsViewController中的序列添加到QuestionsViewController中,并从“属性”检查器中获得序列“标识符名称”。

2-在QuestionsViewController中为该主题添加一个变量,将其命名为“ topicType”。

3-覆盖TopicsViewController中的以下功能,并使用segue发送主题名称:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Identifier Name" {
            if let destinationviewController = segue.destination as? QuestionsViewController , let buttonPressed = sender as? UIButton {
                destinationviewController.topicType = buttonPressed.currentTitle!  
            }
        }
}

4-对于TopicsViewController中的每个按钮,获取按钮操作并在其中键入以下函数:

@IBAction func topicButton(_ sender: UIButton) {

            performSegue(withIdentifier: "Identifier Name", sender: nil)

}

希望这对您有所帮助。