按名称引用多个标签

问题描述 投票:-2回答:2

我有一个标签出口,例如

infoLabel.text = "\(windowText)"

我知道我可以通过更改“”中的文本以及变量windowText中的值来更新标签

但我有多个标签,例如

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

我希望能够更新infoLabel引用text1,text2,text3之一基于一个计数器,通过按下UI按钮增加,但我似乎无法让它工作。

所以如果generalCounter = 3

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

swift uilabel
2个回答
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]
    }
}

0
投票

您可以使用Outlet Collection,UILabel数组来完成每个UILabel,同时保留一个计数器,用于更改文本的数量。

Outlet Collection

© www.soinside.com 2019 - 2024. All rights reserved.