通过名称引用多个标签

时间:2019-02-20 20:38:31

标签: swift uilabel

我有一个标签出口,例如

infoLabel.text = "\(windowText)"

我知道我可以通过更改“”中的文本以及变量 windowText

中的值来更新标签

但是我有多个标签,例如

text1 = "this is text 1"
text2 = "this is text 2"
text3 = "this is text 3"

我希望能够更新 infoLabel 来引用基于计数器的text1,text2,text3之一,该计数器通过按下UIbutton会增加,但是我似乎无法使其正常工作。

如果    generalCounter = 3

如何使我的标签引用名为text(counterGeneral)的字符串 并且如果再次按下并且generalCounter现在为4,则标签现在需要引用text(4)。

2 个答案:

答案 0 :(得分:0)

您可以创建字符串组成的数组

let texts : [String] = [/* Your strings... */]

然后在 Interface Builder操作中相应地更新标签

// The function with is called when a press of your UIButton occur
@IBAction updateLabel(_ sender: UIButton) {

    // Verify there are a next string 
    if generalCounter + 1 < texts.count {

        generalCounter += 1
        infoLabel.text = texts[generalCounter]
    }
}

答案 1 :(得分:0)

您可以使用Outlet Collection(一组UILabel)来简单地遍历每个 同时记下要更改

的文字数量的计数器

Outlet Collection

相关问题