【问题标题】:Update Text in While Loop Swift在 While 循环 Swift 中更新文本
【发布时间】:2019-07-21 12:33:02
【问题描述】:

我的程序有一个 while 循环,它运行一些代码,在执行过程中生成各种文本语句。问题是 UILabel 只打印系列中的最后一行文本(我的理解是因为它迭代得太快了)。如何让标签打印遇到的所有文本,就像在控制台输出中看到的那样?

我查看了此链接,但该示例似乎与我的情况不匹配,并且不确定如何实施修复(如果这甚至是正确的):Update Label In While Loop Swift

class ViewController: UIViewController {

var locationArray = ["Place A", "Place B", "Place C", "Place D"]
var timeStamp = 0
var playerDead = false

@IBOutlet var labelText: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
} //end viewDidLoad

@IBAction func startGame(_ sender: Any) {
var playerOnePosition = locationArray.randomElement()!
var playerTwoPosition = locationArray.randomElement()!

while (playerDead != true) {
    playerOnePosition = locationArray.randomElement()!
    playerTwoPosition = locationArray.randomElement()!
    timeStamp += 1

    if playerOnePosition != playerTwoPosition {
    labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
     } //End first if statement

    if playerOnePosition == playerTwoPosition {
    labelText.text = "\(timeStamp) They are in the same place."
    playerDead = true  //Game ends here
    } //End second if statement
 } //End while loop
 } //End function
 } //End class

通常输出的示例是“13 它们在同一个地方”,但我希望 UIlabel 打印所有其他“时间戳”事件,直到 13。

【问题讨论】:

    标签: swift while-loop label viewcontroller


    【解决方案1】:

    如果要在每次 if 条件为真时添加新的文本行,则需要附加文本并使用换行符,\n

    while (playerDead != true) {
    //...
        if playerOnePosition != playerTwoPosition {
            labelText.text = 
                 (labelText.text ?? "" ) + 
                 "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)\n"
        }
    
        if playerOnePosition == playerTwoPosition {
            labelText.text = (labelText.text ?? "" ) + "\(timeStamp) They are in the same place."
            playerDead = true  
        } 
    }
    

    还要让您的标签显示任意数量的行,将限制设置为 0

    labelText.numberOfLines = 0
    

    【讨论】:

    • 是的!那行得通!我不明白 (labelText.text ?? " ") 部分。那究竟是做什么的? +1 谢谢!
    • text 是一个可选属性,即它可能返回 nil。使用 ??在可选属性之后意味着如果该属性为 nil,它将向右返回值,因此在这种情况下为空字符串(因此我们可以追加)。如果对您有帮助,请将答案标记为已接受
    【解决方案2】:

    您每次都在设置标签的文本,而不是附加到它上面。替换这一行:

    labelText.text = "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
    

    用这个:

    labelText.text += "\(timeStamp) Player One is at \(playerOnePosition) and Player Two is at \(playerTwoPosition)"
    

    并替换这一行:

    labelText.text = "\(timeStamp) They are in the same place."
    

    用这个:

    labelText.text += "\(timeStamp) They are in the same place."
    

    请注意,唯一的区别是我将您的 + 运算符更改为 += 运算符。

    希望这会有所帮助!

    【讨论】:

    • 将“+=”添加到 labelText.text 给我一个错误“表达式类型'@lvalue String?'在没有更多上下文的情况下是模棱两可的”。
    猜你喜欢
    • 2021-12-11
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多