【问题标题】:View Controller not transitioning to next view controller视图控制器未转换到下一个视图控制器
【发布时间】:2021-03-13 01:33:13
【问题描述】:

我正在构建一个测验应用程序,在测验结束时,单击答案后,我希望屏幕切换到显示用户分数的 ResultViewController。我将我的 MCViewController 中的 segue 链接到新的 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()
  }
}

如果有帮助,这里有一点我的照片:

【问题讨论】:

    标签: swift xcode storyboard segue


    【解决方案1】:

    这段代码...

    let resultVC = ResultViewController()
    self.present(resultVC, animated: true, completion: nil)
    

    表示使用init 初始化程序创建的ResultViewController不是来自情节提要。很可能你没有在ResultViewController.init 中写任何东西,因为你在故事板中做了所有的事情,所以它什么都不做并且显示一个空白视图。

    既然你有一个转场,你应该执行那个转场!

    performSegue(withIdentifier: "MCResult", sender: nil)
    

    您可能有一些数据想要传递给ResultViewController。在prepare(for:)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let vc = segue.destination as? ResultViewController {
            // pass whatever you need to "vc"
        }
    }
    

    【讨论】:

    • 我只是在 ResultViewController 中放了一个带有一些文本的标签,然后我使用了 performSegue 行,现在我收到了这个错误:线程 1:“-[Quizzler_iOS13.ResultViewController initWithIdentifier:source:destination:] : 无法识别的选择器发送到实例 0x7fdc1920f6c0"
    • @user13205785 我刚刚意识到你将segue的类设置为ResultViewController...你不应该那样做。您可以将该文本字段留空。请参阅this post 了解更多信息。
    【解决方案2】:

    你在同一个故事板中,所以实例化你的 viewController 就像

    let resultVC = self.storyboard?.instantiateViewController(withIdentifier: "ResultViewControllerID") as! ResultViewController
    self.present(resultVC, animated: true, completion: nil)
    

    我希望你知道 UIViewController 故事板 ID

    【讨论】:

    • 关注link 传递数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多