【问题标题】:Setting the text of a UILabel with a constant使用常量设置 UILabel 的文本
【发布时间】:2020-05-25 00:25:38
【问题描述】:

我是 xCode 和 Swift 的新手,所以如果这个问题很愚蠢,请原谅。 我正在尝试使用按下按钮的 sender.currentTitle 来更新 2 个 UILabel 和一个 UIImageView。

如果用户按下“洪都拉斯”按钮,那么我想用它的相应信息填充 2 UILabel,并用它相应的图像文件填充 UIImageView。 我有第一个 UILabel 工作和 UIImageView 工作,但找不到一种方法来填充第二个 UILabel 与其相应的文本(存储在一个常量中)。

UILabel 1 被称为@IBOutlet weak var countryLabel: UILabel!

UILabel 2 被称为@IBOutlet weak var peopleLabel: UILabel!

UImageView 被称为@IBOutlet weak var peopleUIImageView: UIImageView!

我要传递给这三个的信息/文件名被称为:

“洪都拉斯”

let peopleHondurasText = "The Lenca"

“人民洪都拉斯.png”

我的 UILabel 1 是这样工作的:

countryLabel.text = sender.currentTitle

我通过调用这个函数使图像工作:

func findCountryImages() {
peopleUIImageView.image = UIImage(named: "people\(countryLabel.text! .filter { !" ".contains($0) }).png")
}

我试图像 UIImage 一样调用常量“peopleHondurasText”,但它不起作用。 这是我非常业余的代码:

peopleLabel.text = "people\(countryLabel.text! .filter { !" ".contains($0) })Text"

这里:

"people\(countryLabel.text! .filter { !" ".contains($0) })Text"

应该等于命名的常量:

peopleHondurasText

我希望能够做到这一点,因此如果用户单击任何国家/地区按钮,它将使用该国家/地区的文本和图像填充这 2 个 UILabel 和 UIImageView。

我知道我一定是在做一些根本不正确的事情,或者可能完全错了,但是如果有人可以让我知道如何将常量名称传递给 UILable 或者告诉我你不能这样做,我应该尝试一些不同的东西,我们将不胜感激。

【问题讨论】:

    标签: swift uilabel constants


    【解决方案1】:

    你怎么不只是做:

    peopleLabel.text = peopleHondurasText

    【讨论】:

    • 您好,谢谢。我需要它为所有国家工作,而不仅仅是洪都拉斯。因此,虽然这对洪都拉斯有利,但不适用于其他 200 个国家/地区。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多