在此阶段,应用程序应显示测验结果。因为我希望名称显示在与描述不同的行/不同字体中,所以我将其分成两个标签。目前,两个标签都没有显示出来。我认为这个问题来自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]))
}
}
答案 0 :(得分:0)
如果引用ResultsController
,则表示您未在setUpViews()
中致电viewDidLoad()
。