Update Text in While Loop Swift

两盒软妹~` 提交于 2019-12-02 09:03:47

If you want to add a new line of text for each time an if condition is true you need to append the text and use a new line character, \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  
    } 
}

Also to get your label to display any number of lines set the limit to 0

labelText.numberOfLines = 0

You are setting the text of the label each time, instead of appending to it. Replace this line:

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

with this:

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

and replace this line:

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

with this:

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

Note that the only difference is that I changed your + operators to += operators.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!