gpt4 book ai didi

swift - 结果页面上出现空白屏幕 : where did it go?

转载 作者:行者123 更新时间:2023-11-30 11:32:03 26 4
gpt4 key购买 nike

在此阶段,应用程序应该显示测验的结果。因为我希望名称以与描述不同的行/不同的字体显示,所以我将其分成两个标签。目前,这两个标签都没有出现。我认为这个问题来自 setUpViews 和/或约束,但我不确定。

import UIKit
class ResultsController: UIViewController {

let resultsLabel: UILabel = {
let rlbl = UILabel()
rlbl.text = "Congratulations!"
rlbl.contentMode = .scaleToFill
rlbl.numberOfLines = 0
rlbl.translatesAutoresizingMaskIntoConstraints = false
rlbl.textAlignment = .center
rlbl.font = UIFont(name: "USIS 1949", size: 50)
rlbl.adjustsFontSizeToFitWidth = true
rlbl.numberOfLines=0
rlbl.sizeToFit()
rlbl.minimumScaleFactor = 0.1
return rlbl
}()

let descriptionLabel: UILabel = {
let dlbl = UILabel()
dlbl.text = "Congratulations!"
dlbl.contentMode = .scaleToFill
dlbl.numberOfLines = 0
dlbl.translatesAutoresizingMaskIntoConstraints = false
dlbl.textAlignment = .justified
dlbl.font = UIFont(name: "Flama", size: 30)
return dlbl
}()

func setupViews() {
self.view.addSubview(resultsLabel)
resultsLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
resultsLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
resultsLabel.widthAnchor.constraint(equalToConstant: 500).isActive=true

self.view.addSubview(descriptionLabel)
descriptionLabel.topAnchor.constraint(equalTo: resultsLabel.bottomAnchor, constant: 20).isActive=true
descriptionLabel.heightAnchor.constraint(equalToConstant: 50).isActive=true
descriptionLabel.widthAnchor.constraint(equalToConstant: 150).isActive=true
descriptionLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
descriptionLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
}

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(done(sender:)))

navigationItem.title = "Results"

view.backgroundColor = UIColor.white

let names = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]

let additional = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]

var score = 0
for question in QuestionController.questionsList {
score += question.selectedAnswerIndex!
}

let result = names[score % names.count]
resultsLabel.text = "\(result)"

let description = additional[score % additional.count]
descriptionLabel.text = "\(description)"

}

@objc func done(sender: UIBarButtonItem) {

navigationController?.popToRootViewController(animated: true)

}

}

class QuestionHeader: UITableViewHeaderFooterView {

override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
setupViews()
}

let nameLabel: UILabel = {
let label = UILabel()
label.text = "Sample Question"
label.font = UIFont.boldSystemFont(ofSize: 30)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

func setupViews() {
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

class AnswerCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let nameLabel: UILabel = {
let label = UILabel()
label.text = "Sample Answer"
label.font = UIFont.systemFont(ofSize: 30)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

func setupViews() {
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}

}

最佳答案

如果引用 ResultsController,您不会在 viewDidLoad() 中调用 setUpViews()

关于swift - 结果页面上出现空白屏幕 : where did it go?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50159931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com