显示UILabel的文字 [英] Showing UILabel's text

查看:51
本文介绍了显示UILabel的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 iOS编程:Big Nerd Ranch指南,第6版的指南,我尝试快速制作一个Quiz项目.

Following the iOS Programming: The Big Nerd Ranch Guide, 6th Edition 's guide, I try to make a Quiz project using in swift.

此处的ViewController代码

The ViewController code here

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除外.

But running with non visual text except the int one.

推荐答案

不要将 questionLabel answerLabel 的宽度限制为特定数字.尝试正确给出约束,例如标签宽度等于viewController的宽度,如下面的屏幕截图所示.

Don't constraint the width of questionLabel and answerLabel to specific number. Try to give the constraints properly like label width is equal to viewController's width like in the screen shot below.

这篇关于显示UILabel的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆