ios - 显示 UILabel 的文本

标签 ios swift

按照 iOS 编程:The Big Nerd Ranch 指南,第 6 版 的指南,我尝试使用 swift 制作一个测验项目。

ViewController代码在这里

import UIKit

class ViewController: UIViewController {
    @IBOutlet var questionLabel: UILabel!
    @IBOutlet var answerLabel: UILabel!

    let questions: [String] = [
        "What is 7+7?",
        "What is the capital of Vermont?",
        "What is cognac made from?"
    ]
    let answers: [String] = [
        "14",
        "Montpelier",
        "Grapes"
    ]
    var currentQuestionIndex: Int = 0

    @IBAction func showNextQuestion(_ sender: UIButton) {
        currentQuestionIndex += 1
        if currentQuestionIndex == questions.count {
            currentQuestionIndex = 0
        }

        let question: String = questions[currentQuestionIndex]
        questionLabel.text = question
        answerLabel.text = "???"
    }

    @IBAction func showAnswer(_ sender: UIButton) {
        let answer: String = answers[currentQuestionIndex]
        answerLabel.text = answer
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        questionLabel.text = questions[currentQuestionIndex]
    }
}

但除了 int 之外,使用非可视文本运行。

enter image description here enter image description here

最佳答案

我已经用演示项目检查了你的代码,它工作正常。你的问题是你在 storyboard 中添加的 UI components

为您的 questionLabel 分配约束,如下图所示:

enter image description here

基本上我已经分配了 Leading, Trailing TopHeight 约束来标记和设置 Alignment 中心

其他标签和按钮也一样。而且看起来会很好。

更多信息请查看THIS带有您的代码的演示项目。

关于ios - 显示 UILabel 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086194/

相关文章:

html - 如何禁用 iOS Chrome 密码/信用卡自动填充?

ios - MonoTouch项目导致Application Loader报错 "icon dimensions (0 x 0) don' t meet the size requirements”

javascript - Phonegap 设备准备好在 iOS 中使用 Cordova 2.2.0 不触发

swift - 如何获得 View 的中心

swift - StackView 在所有屏幕尺寸上显示不正确

html - CSS 空白 :pre in Cordova application not working

xcode - 如果我在应用商店购买XCode 4,我可以做iOS开发吗?

ios - 如何从一维条码图像解码

ios - Xcode 6 中 UILabel 的属性字符串

ios - 无法从 JSONDecoder 获取数据,没错,我编的结构?