【问题标题】:Append same text to label when button is pushed multiple times多次按下按钮时将相同的文本附加到标签
【发布时间】:2017-08-17 18:45:44
【问题描述】:

我想在按下按钮时将文本附加到标签上。当再次按下按钮时,我希望在标签的新行中添加相同的文本。

var someText = "text"

@IBAction func button1(_ sender: UIButton) {
    label.text = someText
}

当我这样做时,标签只会打印: 文本。

如果按钮被按下 3 次,我希望标签打印:“text” - new line - “text” - new line - “text”

或者。
3x 文字

我应该怎么做呢?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    正如马特所说,您需要使用编程。只需获取标签中的字符串并使用新文本追加新行。我没有测试这段代码,但它会是这样的:

    // variable for the "text"
    var someText = "text"
    
    @IBAction func button1(_ sender: UIButton) {
        // if the label text is empty, do not append a new line character
        if label.text?.isEmpty {
            label.text = someText
        } else {
            // if the label contains text, append a new line character and add the new text
            label.text += "\n" + someText
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多