Swift 视图控制器未转换到下一个视图控制器

Swift 视图控制器未转换到下一个视图控制器,swift,xcode,storyboard,segue,Swift,Xcode,Storyboard,Segue,我正在构建一个测验应用程序,在测验结束时,单击一个答案,我希望屏幕切换到ResultViewController,在那里显示用户的分数。我将MCViewController中的一段链接到新的ResultViewController屏幕。但是在完成测试后,屏幕有点暗,没有错误 MCViewController: import UIKit class MCViewController: UIViewController { @IBOutlet weak var questionLabel: U

我正在构建一个测验应用程序,在测验结束时,单击一个答案,我希望屏幕切换到ResultViewController,在那里显示用户的分数。我将MCViewController中的一段链接到新的ResultViewController屏幕。但是在完成测试后,屏幕有点暗,没有错误

MCViewController:

 import UIKit

class MCViewController: UIViewController {

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var aButton: UIButton!
@IBOutlet weak var bButton: UIButton!
@IBOutlet weak var cButton: UIButton!
@IBOutlet weak var dButton: UIButton!

let quiz =
[
    Questions(q: "When did English settlement begin in Canada?", a: "1510", b: "1497", c: "1604", d: "1720", answer: "1604"),
    Questions(q: "Who passed the Quebec Act of 1774?", a: "Canadian Parliament", b: "British Parliament", c: "Quebec Parliament", d: "The French majority", answer: "British Parliament"),
    Questions(q: "Whose portrait is on the Canadian 10 dollar bill?", a: "Sir George Cartier", b: "Sir Etienne Tache", c: "Sir John A. Macdonald", d: "Sir Louis La Fontaine", answer: "Sir John A. Macdonald"),
    Questions(q: "What are the responsibilities of the federal government?", a: "Matters of national and international concern.", b:  "Matters of national concern.", c: "Matters of international concern.", d: "Matters of provincial concern.", answer: " Matters of national and international concern."),
    Questions(q: "What is 'Habeas corpus'?", a: "The right to challenge unlawful detention by the state.", b: "The right to live and work anywhere in Canada.", c: "The right to speak freely.", d: " The right for peaceful assembly.", answer: "The right to challenge unlawful detention by the state.")
]

var questionNumber = 0

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

@IBAction func answerPressed(_ sender: UIButton) {
    
    let userAnswer = sender.currentTitle
    let actualAnswer = quiz[questionNumber].answer
    
    if (userAnswer == actualAnswer) {
        sender.backgroundColor = UIColor.green
    } else {
        sender.backgroundColor = UIColor.red   //END OF ARRAY, SHOULD TRANSITION TO RESULTVIEWCONTROLLER
    }
    
    if (questionNumber + 1 < quiz.count){
        questionNumber += 1
    } else {
        let resultVC = ResultViewController()
        self.present(resultVC, animated: true, completion: nil)
    }
    Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
  }

   @objc func updateUI() {
    questionLabel.text = quiz[questionNumber].q
    aButton.setTitle(quiz[questionNumber].a, for: .normal)
    bButton.setTitle(quiz[questionNumber].b, for: .normal)
    cButton.setTitle(quiz[questionNumber].c, for: .normal)
    dButton.setTitle(quiz[questionNumber].d, for: .normal)
    aButton.titleLabel?.adjustsFontSizeToFitWidth = true;
    bButton.titleLabel?.adjustsFontSizeToFitWidth = true;
    cButton.titleLabel?.adjustsFontSizeToFitWidth = true;
    dButton.titleLabel?.adjustsFontSizeToFitWidth = true;
    aButton.backgroundColor = UIColor.clear
    bButton.backgroundColor = UIColor.clear
    cButton.backgroundColor = UIColor.clear
    dButton.backgroundColor = UIColor.clear
    progressBar.progress = Float(questionNumber + 1) / Float(quiz.count)
}
}
import UIKit

class ResultViewController : UIViewController{
override func viewDidLoad() {
    super.viewDidLoad()
  }
}
以下是我的一些图片,如果有帮助的话:

此代码

let resultVC = ResultViewController()
self.present(resultVC, animated: true, completion: nil)
显示一个
ResultViewController
,它是使用
init
初始化器创建的,而不是从故事板创建的。很可能您没有在
ResultViewController.init
中编写任何内容,因为您在故事板中完成了所有操作,所以它什么也不做,并显示一个空白视图

既然你有一个segue,你应该改为执行这个segue

performSegue(withIdentifier: "MCResult", sender: nil)
您可能有一些数据要传递给
ResultViewController
。在
准备(for:)
中执行此操作:

此代码

let resultVC = ResultViewController()
self.present(resultVC, animated: true, completion: nil)
显示一个
ResultViewController
,它是使用
init
初始化器创建的,而不是从故事板创建的。很可能您没有在
ResultViewController.init
中编写任何内容,因为您在故事板中完成了所有操作,所以它什么也不做,并显示一个空白视图

既然你有一个segue,你应该改为执行这个segue

performSegue(withIdentifier: "MCResult", sender: nil)
您可能有一些数据要传递给
ResultViewController
。在
准备(for:)
中执行此操作:


您在同一个故事板中,因此,实例化viewController如下

让resultVC=self.storyboard?.instanceeviewcontroller(带有标识符:“ResultViewControllerID”)为!结果视图控制器
self.present(resultVC,动画:true,完成:nil)

我希望您知道UIViewController情节提要ID,因为您在同一情节提要中,所以请像下面这样实例化您的viewController

让resultVC=self.storyboard?.instanceeviewcontroller(带有标识符:“ResultViewControllerID”)为!结果视图控制器
self.present(resultVC,动画:true,完成:nil)

我希望您知道UIViewController情节提要ID

我只是在ResultViewController中添加了一个带有一些文本的标签,并使用了performSegue行,现在我得到了以下错误:线程1:“-[Quizzler_iOS13.ResultViewController initWithIdentifier:源:目标:]:未识别的选择器发送到实例0x7fdc1920f6c0”@用户13205785我刚刚意识到您将segue的类设置为
ResultViewController
。。。你不应该那样做。您可以将文本字段留空。请参阅以获取更多信息。我只是在ResultViewController中放置了一个带有一些文本的标签,并使用了PerformScheue行,现在我得到了以下错误:线程1:“-[Quizzler_iOS13.ResultViewController initWithIdentifier:源:目标:]:未识别的选择器发送到实例0x7fdc1920f6c0”@用户13205785我刚刚意识到您将segue的类设置为
ResultViewController
。。。你不应该那样做。您可以将文本字段留空。有关更多信息,请参阅。按照此传递数据按照此传递数据